egrep -w command by reading from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting egrep -w command by reading from file
# 1  
Old 10-03-2011
egrep -w command by reading from file

Dear All,

I want to get the rows from file1.txt whose first column is exactly as the numbers (not subset) from file1.txt. so, i have to read by line from file1.txt and search in file2.txt. So the first column of the output must be the same as the file1.txt.

I used
Code:
% egrep -w -f file1.txt file2.txt > output.txt

However, it doesn't give the output that i want.

I have a file1.txt which is lines having names/numbers like (in order):
Code:
1130298
1813782
1882185
2025239
2059032

and i have a second file file2.txt
Code:
72434 0.0015000000 0
78032 0.0015000000 0.0000083970
554461 0.0015000000 0.0007230405
...
1130298 0.7079904023 0.7144873123
1133921 0.7424346556 0.7170611862
1136648 0.8391464179 0.7190858056
...
1813782 0.2660857424 1.1187764206
1829898 1.1420843336 1.1230646584
1833906 2.0485655693 1.1276421324
...

OUTPUT.txt
Code:
1130298 0.7079904023 0.7144873123
1813782 0.2660857424 1.1187764206
...

Thanks in advance,

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 10-03-2011 at 11:18 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-03-2011
Does your grep version support the -w or -f option? Another approach could be:
Code:
awk 'NR==FNR{a[$0];next}$1 in a' file1.txt file2.txt > output.txt

# 3  
Old 10-03-2011
Yes, this command worked. Could you please explain the details of 'NR==FNR{a[$0];next}$1 in a' ?

Thanks in advance,
# 4  
Old 10-03-2011
Code:
egrep -f File1.txt File2.txt

# 5  
Old 10-04-2011
Quote:
Originally Posted by senayasma
Yes, this command worked. Could you please explain the details of 'NR==FNR{a[$0];next}$1 in a' ?

Thanks in advance,
Sure.
Code:
awk 'NR==FNR{a[$0];next}$1 in a' file1.txt file2.txt > output.txt

Explanation:

NR==FNRcondition is true when we read the first file
{a[$0];next}Define array a with the line of the first file as index
$1 in aprint the line of the second file if the first field is an index of arrary a.
# 6  
Old 10-12-2011
Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading from file bash command

Hello, I have a file in the following format id sample platform R1 R2 gene1 gene2 gene3 1 abc llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp asp 2 def llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp 3 ghi llumina ... (3 Replies)
Discussion started by: nans
3 Replies

2. UNIX for Beginners Questions & Answers

Egrep command is not working for me

my file is below REREGISTER is something to Failed to create the request Failed to create the request in not easy I know how REREGISTERcommand i run is egrep 'REREGISTER|Failed|to|create|the|request' test1 expected output REREGISTER is something to Failed to create the request i should... (2 Replies)
Discussion started by: mirwasim
2 Replies

3. Shell Programming and Scripting

SendEmail - Script reading database file with sleep command

Hello, I would like to send email message to my mail list. I have been running linux based server and I submitted this process manually up to now. I would like to send each individual with a shell script. In ssh panel, I tested below command and it works smoothly. sendEmail -t... (1 Reply)
Discussion started by: baris35
1 Replies

4. Shell Programming and Scripting

help in egrep command in file

Hi Guys, I need hepl on egrep commnad I have one file and i need grep only specific lines.. EX: 2012-04-01 02:15:14 w 2012-04-01 02:15:14 w 2012-04-01 02:15:14 w 2012-10-26 02:15:14 w 2012-04-01 02:15:14 w 2012-10-26 02:15:14 w 2012-10-26 02:15:14 w 2012-10-26 03:18:56 M... (1 Reply)
Discussion started by: asavaliya
1 Replies

5. Shell Programming and Scripting

Help with egrep command

Hello folks, Here's how my current egrep command works: egrep "NY|DC|LA|VA|MD" state_data.txt I am planning to use a file to enter all allowable state values like say a new state_names.lookup with the following data: NY DC LA VA MD egrep "`cat state_names.lookup`"... (6 Replies)
Discussion started by: calredd
6 Replies

6. Shell Programming and Scripting

Help with egrep command

cat /tmp/inventory.csv|grep AARP|egrep -v "T11|12.4\(7\)" how do i exclude in addition to above 12.4\(3\) I have tried adding this in i.e -v "T11|12.4\(7\)|12.4\(3\)" but it did not work (3 Replies)
Discussion started by: slashbash
3 Replies

7. Shell Programming and Scripting

egrep command and order

Hi All, Here is my question. I have two files, file1.txt and file2.txt. I need the line number (index number) of file2.txt where the words in file1.txt appear. But they have to be in the same order as file1.txt. In example, file1.txt Z K A ... T file2.txt W A Q R (6 Replies)
Discussion started by: senayasma
6 Replies

8. Shell Programming and Scripting

Query regarding egrep command

Hi All, I am having a query regarding the usage of egrep command. i am having two unix environmanets in environment when i am using "egrep -f" it is working fine and other unix environment i am getting a syntax error. Please let me know if i need to set any environmane variables. ... (12 Replies)
Discussion started by: Sriram.Vedula53
12 Replies

9. Shell Programming and Scripting

Help with egrep command

Hi All, I am using egrep command to search one pattern. Following is the command i am using egrep -i "ACL*" filename but its also giving me the records which do not contain ACL. any help would be appreciated. Regards, Sam (3 Replies)
Discussion started by: sam25
3 Replies

10. Shell Programming and Scripting

egrep command

I'd like to grep a pattern of a version number as *_number.number.number number should be digit my grep is |egrep '^*++\.+' It works for V_3.2.1 or V _5.3.2 but not with V_43.6.543 !!!!! How can I specify any repetition of digit in the ? thanks, (4 Replies)
Discussion started by: annelisa
4 Replies
Login or Register to Ask a Question