This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to extract a entire row based on a single key word from a csv or excel file?

I would like to extract a row from a excel sheet based on a key word (string). So I have used the following command in R x = data.frame(read.csv("t3.csv")) y = grep("avrbs2|avrbs3|xop", x$Gene_Name.ID) z = x[y, ] It extract the entire row based on the key word. However I need to skip the redundancy of the keyword. For example the avrbs2 and avrbs3 repeated over 50 to 60 times in the excel/csv sheet, from that I need to extract the same without redundancy. for example, I have a data like this in my excel sheet

avrbs3_AAM39226_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3  198 100 66  0   0   822 887 2   199 2.45E-37    131
avrbs3_AAM39226_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3  250 100 21  0   0   867 887 2   64  3.44E-05    45.4
avrbs3_AAM39226_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3  113 100 15  0   0   1   15  46  2   0.005   34.7
avrbs3_AAM39243_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3  265 100 260 0   0   837 1096    2   781 8.91E-158   467
avrbs3_AAM39243_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3  198 100 66  0   0   792 857 2   199 2.38E-37    131
avrbs3_AAM39243_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3  250 100 21  0   0   837 857 2   64  3.42E-05    45.4
avrbs3_AAM39243_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3  113 100 15  0   0   1   15  46  2   0.004   35
avrbs3_AAM39261_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3  198 100 66  0   0   792 857 2   199 2.67E-37    131

From this, I need to extract the first row which is having the key word avrbs3, like wise I have to extract other key word containg rows aswell. Therefore, Please help me to do the same in R or shell script etc.,

r script perl python blast

Don't understand this part.

For example the avrbs2 and avrbs3 repeated over 50 to 60 times in the excel/csv sheet, from that I need to extract the same without redundancy.

Do you mean you want to extract first occurrence only ? Can you put sample input and sample output ?

Dear Chirag, As you mentioned, exactly I need to extract the first occurrence.

sample in

avrbs3_AAM39226_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3 198 100 66 0 0 822 887 2 199 2.45E-37 131 avrbs3_AAM39226_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3 250 100 21 0 0 867 887 2 64 3.44E-05 45.4 avrbs3_AAM39226_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3 113 100 15 0 0 1 15 46 2 0.005 34.7 avrbs3_AAM39243_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3 265 100 260 0 0 837 1096 2 781 8.91E-158 467 avrbs3_AAM39243_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3 198 100 66 0 0 792 857 2 199 2.38E-37 131 avrbs2_AAM39243_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs2 250 100 21 0 0 837 857 2 64 3.42E-05 45.4 avrbs3_AAM39243_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3 113 100 15 0 0 1 15 46 2 0.004 35 avrbs2_AAM39261_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306 198 100 66 0 0 792 857 2 199 2.67E-37 131 avrbs2_AAM39261_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306 250 100 21 0 0 837 857 2 64 3.5E-05 45.4 avrbs2_AAM39261_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306 113 100 15 0 0 1 15 46 2 0.004 35 avrbs3_AAM39311_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3 198 100 66 0 0 859 924 2 199 3.07E-37 131 avrbs3_AAM39311_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3 250 100 21 0 0 904 924 2 64 3.76E-05 45.4 avrbs3_AAM39311_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3 113 100 15 0 0 1 15 46 2 0.004 35 avrbs3_AAW74491_1_avirulence_protein_Xanthomonas_oryzae_pv_oryzae_KACC_10331_avrbs3 250 100 21 0 0 805 825 2 64 4.74E-05 45.1

Sample out

avrbs3_AAM39226_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs3  198 100 66  0   0   822 887 2   199 2.45E-37    131
avrbs2_AAM39243_1_avirulence_protein_plasmid_Xanthomonas_citri_pv_citri_str_306_avrbs2  250 100 21  0   0   837 857 2   64  3.42E-05    45.4

Hi, sounds like Unix grep would be just as fine. Also, this is not bioinformatics, is it?

Try: grep -e "keyword" | head -1

This really is the correct answer, because it's even idiomatic Unix programming, but if I could offer a suggestion, since grep, sed, and awk are going to be processing line by line, while this answer will yield the right answer, you won't get it until grep is finished processing the entire dataset. I would recommed doing something like this,

awk '/avrbs3/ { print; exit; }' ./filename

where now the process is immediately terminated after hitting the first instance of the target string.

I thought that will also work with the pipe to head -1. Sorry, no ;)

better to simply use grep -m 1 to stop reading after the first match.

I did not for a second consider that exiting right away meant the end of the pipe, wow, you are absolutely right; my Windows roots have betrayed me

Dear Jflopezfernandez, Thank you for your help, It works for single string, but how to to do the same for multiple strings.

Oh, I thought you only needed to get it the first time. If you need to keep matching it over and over, you can do that a few different ways. You can use Dr. Dondrup's method, just simply take out the second part where you pipe to 'head', you can use my method without calling exit, or you can use sed. All three will give you the same result, like this:

grep -e 'avrbs3' ./file
sed -n -e '/avrbs3/p' ./file
awk '/avrbs3/ { print; }' ./file

This is a pretty contrived example though, so if you need any more help let me know.

Dear jflopezfernandez, I have three key words and tried the command line as given below

grep -e 'avrbs3/avrbs2/xop' ./119.csv | head -1 > 119single.csv

But, I could not get the result. Since it works for single string very well. Please help me to do the same for multiple strings

Remember that a regular expression is written as /something/, so your regex doesn't make sense. You can either split them up into their own individual regex like this:

sed -n -e '/avrbs3/p;/avrbs2/p;/xop/p' ./file'

or, more logically in this case, simply group the strings with parentheses and use an OR op, like this:

sed -n -E -e '/(avrbs3|avrbs2|xop)/p' ./file

With grep it would look like this:

grep -E -e '(avrbs3|avrbs2|xop)' ./file

Note that grep expects a regex by default, so the forward slashes that brace a regular expression are implied, while that is not true of sed.

Thank you Jflopezfernandez for your detailed answer. However, it prints all the occurrence of the key word. I need to extract only first occurrence of the key word from the excel file. If I pipe "head -1 command" which prints the first occurrence of the first keyword only.

Dear Michael, Thank you for your help, It works for single string, but how to to do the same for multiple strings.

Hello Dineshkumar K!

We believe that this post does not fit the main topic of this site.

General programming query, please search stackoverflow and similar sites.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

Dear healey, Its not a bioinformatic analysis, however this kind of data mining has the important role in bioinformatics analysis. I am new to this community and this field as well, that is why I was not aware of this community main focus topics. If you feel that, my question distracting the focus of this community members, you are welcome to close my question. My kind request to you, please keep my question open till I get the answer. Once I get my answer, my self I will delete the same. Hereafter, I would post this kind of question on stackoverflow site or elsewhere. Thank you.

It's fine to be new in the community (closing the question is in no way to be taken personally). We simply try to discourage questions such as this which are fairly easily solved via google/stackoverflow etc. While it is biological in nature, the actual task you need to perform requires no specialist bioinformatics knowledge, and is therefore considered off-topic.

We do not want you to delete the thread (else we'd have already done that), we are just trying to avoid establishing a precedent, so I'm afraid the post is going to remain closed.

Ok healey.

0 answers

No answers yet.

Log in to answer this question.