grep multiple rows from file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep multiple rows from file.
# 1  
Old 09-13-2009
grep multiple rows from file.

Hi, I have file1 that contains many columns as show the first three below:
Code:
"At1g29930"     198     2105
"At5g46430"     5569     9576
"At1g64740"     1908     2505
"At5g46430"     6717     11317
"At1g64740"     453     655
"At1g12470"     33     18
"At1g80680"     149     262
"At1g23040"     6351     2620
"At1g31390"     -18     87
"At5g19860"     -30     68
"At1g80470"     -49     85
"IC-At1g29930-1 um"     2203     1713
"IC-At1g29930-5 um"     4873     3668
"At5g19860.1"     90     132
"At5g20520"     6     81
"At1g80680.1"     123     186

What I want is to extract the rows from file1 by the first column that contains my following list in file2:

Code:
At5g46430
At1g64740
At1g12470
At1g80680
At1g23040
At1g31390
At5g19860
At1g80470
At1g29930
At5g20520

Members in file2 are unique but in file1 they may have multiple copies or iso-forms, like At1g80680 and At1g80680.1. My challenge is to get the rows in file1 that matches exactly by

Code:
grep -wf file2 file1 > file3;

If there is multiple match, get one (or the first one?) and ignore the rest (eg At1g29930, not IC-At1g29930-1 um or IC-At1g29930-5 um), but it did not work. Any help is highly appreciated!

Last edited by vgersh99; 09-14-2009 at 09:51 AM.. Reason: code tags, PLEASE!
# 2  
Old 09-14-2009
something like this,

$sed -n 's/\"\(At[^ \.]*\)\" .*/\1/gp' input | sort -u
At1g12470
At1g23040
At1g29930
At1g31390
At1g64740
At1g80470
At1g80680
At5g19860
At5g20520
At5g46430
# 3  
Old 09-14-2009
Code:
 
awk -v t="\"" '/^\"?At/&&NR==FNR{gsub(t,"",$1);a[$1]=1;next}
 {if(a[$1]) print}' file1 file2

# 4  
Old 09-14-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

2. Shell Programming and Scripting

Grep and replace multiple strings in a file with multiple filenames in a file

Hi, I have a file containing list of strings like i: Pink Yellow Green and I have file having list of file names in a directory j : a b c d Where j contains of a ,b,c,d are as follows a: Pink (3 Replies)
Discussion started by: madabhg
3 Replies

3. Shell Programming and Scripting

How to get multiple rows from a file?

Gents, I have a big file, where I would like to get some rows.. Then file1 contains. Obs_Report_Result : Shot_Report : ... (4 Replies)
Discussion started by: jiam912
4 Replies

4. Shell Programming and Scripting

awk to grep rows by multiple fields

Hello, I met a challenge to extract part of the table. I'd like to grep the first three matches based on field1 and field2. Input: D A 92.85 1315 83 11 D A 95.90 757 28 3 D A 94.38 480 20 7 D A 91.21 307 21 6 D A 94.26 244 ... (6 Replies)
Discussion started by: yifangt
6 Replies

5. UNIX for Dummies Questions & Answers

Write the total number of rows in multiple files into another file

Hello Friends, I know you all are busy and inteligent too... I am stuck with one small issue if you can help me then it will be really great. My problem is I am having some files i.e. Input.txt1 Input.txt2 Input.txt3 Now my task is I need to check the total number of rows in... (4 Replies)
Discussion started by: malaya kumar
4 Replies

6. Shell Programming and Scripting

connecting to table to extract multiple rows into file from unix script

I need to extract the data from oracle table and written the below code. But it is not working.There is some problem with the query and output is shown is No rows selected" . If I run the same query from sql developer there is my required output. And if I run the shell script with simple sql... (7 Replies)
Discussion started by: giridhar276
7 Replies

7. Shell Programming and Scripting

Split single rows to multiple rows ..

Hi pls help me out to short out this problem rm PAB113_011.out rm: PAB113_011.out: override protection 644 (yes/no)? n If i give y it remove the file. But i added the rm command as a part of ksh file and i tried to remove the file. Its not removing and the the file prompting as... (7 Replies)
Discussion started by: sri_aue
7 Replies

8. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

9. UNIX for Dummies Questions & Answers

Converting rows into multiple-rows

Hi every one; I have a file with 22 rows and 13 columns which includes floating numbers. I want to parse the file so that every five columns in the row would be a new record (row). For example, the first line in the old file should be converted into three lines with first two lines contain 5... (6 Replies)
Discussion started by: PHL
6 Replies

10. Shell Programming and Scripting

How to handle multiple rows in a file

I have a FILE a.txt which has the following data 113901.94,113901.94,56950.97,56950.97,NOT MATCHING,NOT MATCHING 10693.04,10693.04,5346.52,5346.52,NOT MATCHING,NOT MATCHING 1901.94,1901.94,550.97,550.97,NOT MATCHING,NOT MATCHING 103.04,103.04,53.52,53.52,NOT MATCHING,NOT MATCHING #### This... (2 Replies)
Discussion started by: ksmbabu
2 Replies
Login or Register to Ask a Question