Grep Line with Matching Fields


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Grep Line with Matching Fields
# 8  
Old 08-09-2007
vgersh99,

The sample data might not have a match of file1 in file2.

Code:
file1:

1000FAAA,100706,11446
1000FAAA,1067050,12239
1000FAAA,1081989,24010
1000FAAA,111273,26019
1000FAAA,1130922,13608
1000FAAA,11554,10214
1000FAAA,1424483,11564
1000FAAA,160237,8409
1000FAAA,255794,29390
1000FAAA,264869,8663

file2:

000000AR8894426,13443,AR8894,1FAAA,0,11233
000000AR8967426,13443,AR8967,1FAAA,11233,14800
000000AR8993426,13443,AR8993,1FAAA,26033,8750
000000AR9012426,13443,AR9012,1FAAA,34783,8000
000000AR9067426,13443,AR9067,1FAAA,42783,11576
000000AR9203426,13443,AR9203,1FAAA,54359,9957
000000AR9570426,13443,AR9570,1FAAA,64316,9228
000000AW6599426,13443,AW6599,1FAAA,73544,10703
000000AW6609426,13443,AW6609,1FAAA,84247,19952
000000AW6617426,13443,AW6617,1FAAA,104199,8632

# 9  
Old 08-09-2007
Quote:
Originally Posted by vgersh99
hem.awk:
Code:
FNR==NR { f1[$0]; next}
( $(NF-2) FS $(NF-1) FS $(NF) ) in f1

Quote:
Originally Posted by hemangjani
hem.awk:
FNR==NR { f1[$0]; next}
( $(NF-2) FS $(NF-1) FS $(NF) ) in file1
hemangjani's code does not match vgersh99's code
# 10  
Old 08-09-2007
Quote:
Originally Posted by hemangjani
vgersh99,

The sample data might not have a match of file1 in file2.

Code:
file1:

1000FAAA,100706,11446
1000FAAA,1067050,12239
1000FAAA,1081989,24010
1000FAAA,111273,26019
1000FAAA,1130922,13608
1000FAAA,11554,10214
1000FAAA,1424483,11564
1000FAAA,160237,8409
1000FAAA,255794,29390
1000FAAA,264869,8663

file2:

000000AR8894426,13443,AR8894,1FAAA,0,11233
000000AR8967426,13443,AR8967,1FAAA,11233,14800
000000AR8993426,13443,AR8993,1FAAA,26033,8750
000000AR9012426,13443,AR9012,1FAAA,34783,8000
000000AR9067426,13443,AR9067,1FAAA,42783,11576
000000AR9203426,13443,AR9203,1FAAA,54359,9957
000000AR9570426,13443,AR9570,1FAAA,64316,9228
000000AW6599426,13443,AW6599,1FAAA,73544,10703
000000AW6609426,13443,AW6609,1FAAA,84247,19952
000000AW6617426,13443,AW6617,1FAAA,104199,8632

given your initial description and those 2 sample files - there're no matches.
What would your expected output be?

Post a set of files with the expected output
# 11  
Old 08-09-2007
vgersh99,

The previous sample was the last 10 lines from both files.
For the below sample, I made sure that there few matches.

Code:
file1:

1000FAAA,100706,11446
1000FAAA,1067050,12239
1000FAAA,1081989,24010
1000FAAA,111273,26019
1000FAAA,1130922,13608
1000FAAA,11554,10214
1000FAAA,1424483,11564
1000FAAA,160237,8409
1000FAAA,255794,29390
1000FAAA,264869,8663

file2:

000000E8F900496,13492,E8F900,1000FAAA,100706,11446
0000001768X0496,13492,1768X0,1000FAAA,1067050,12239
000000354923496,13492,354923,1000FAAA,1081989,24010
00000029R018496,13492,29R018,1000FAAA,1424483,11564
0000000Y495R496,13492,0Y495R,1000FAAA,160237,8409
0000003W5R34496,13492,3W5R34,1000FAAA,255794,29390
0000002AA859496,13492,2AA859,1000FAAA,264869,8663
000000E70311496,13492,E70311,1000FAAA,309934,30127
000000R99462496,13492,R99462,1000FAAA,394836,8279
000000063EW7496,13492,063EW7,1000FAAA,421058,10314
0000000960FF496,13492,0960FF,1000FAAA,437530,11282
000000351795496,13492,351795,1000FAAA,513053,22251

Output:

000000E8F900496,13492,E8F900,1000FAAA,100706,11446
0000001768X0496,13492,1768X0,1000FAAA,1067050,12239
000000354923496,13492,354923,1000FAAA,1081989,24010
00000029R018496,13492,29R018,1000FAAA,1424483,11564
0000000Y495R496,13492,0Y495R,1000FAAA,160237,8409
0000003W5R34496,13492,3W5R34,1000FAAA,255794,29390
0000002AA859496,13492,2AA859,1000FAAA,264869,8663

# 12  
Old 08-09-2007
Try this:
cat file1 | while read line
do
grep $line file2 >> file3

done
# 13  
Old 08-09-2007
Quote:
Originally Posted by hemangjani
vgersh99,

The previous sample was the last 10 lines from both files.
For the below sample, I made sure that there few matches.

Code:
file1:

1000FAAA,100706,11446
1000FAAA,1067050,12239
1000FAAA,1081989,24010
1000FAAA,111273,26019
1000FAAA,1130922,13608
1000FAAA,11554,10214
1000FAAA,1424483,11564
1000FAAA,160237,8409
1000FAAA,255794,29390
1000FAAA,264869,8663

file2:

000000E8F900496,13492,E8F900,1000FAAA,100706,11446
0000001768X0496,13492,1768X0,1000FAAA,1067050,12239
000000354923496,13492,354923,1000FAAA,1081989,24010
00000029R018496,13492,29R018,1000FAAA,1424483,11564
0000000Y495R496,13492,0Y495R,1000FAAA,160237,8409
0000003W5R34496,13492,3W5R34,1000FAAA,255794,29390
0000002AA859496,13492,2AA859,1000FAAA,264869,8663
000000E70311496,13492,E70311,1000FAAA,309934,30127
000000R99462496,13492,R99462,1000FAAA,394836,8279
000000063EW7496,13492,063EW7,1000FAAA,421058,10314
0000000960FF496,13492,0960FF,1000FAAA,437530,11282
000000351795496,13492,351795,1000FAAA,513053,22251

Output:

000000E8F900496,13492,E8F900,1000FAAA,100706,11446
0000001768X0496,13492,1768X0,1000FAAA,1067050,12239
000000354923496,13492,354923,1000FAAA,1081989,24010
00000029R018496,13492,29R018,1000FAAA,1424483,11564
0000000Y495R496,13492,0Y495R,1000FAAA,160237,8409
0000003W5R34496,13492,3W5R34,1000FAAA,255794,29390
0000002AA859496,13492,2AA859,1000FAAA,264869,8663

Given the sample files above and an awk solution, I get the exact desired output as posted.
# 14  
Old 08-10-2007
Bug

vgersh99,

Smilie Worked like a charm. Thanks a lot. All it took was 3 minutes to rip through. Thanks a lot.

Yesterday I make a mistake in my hem.awk file. Kahuna pointed out the mistake that I was using file1 instead of f1, due to my ignorance about the code.

I would appreciate if you could explain briefly what exactly is happening in the code. This could also help me in future tasks while working with huge data.

Thanks a lot.

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to control grep output intact for each matching line?

I have multiple (~80) files (some can be as big as 30GB of >1 billion of lines!) to grep on a pattern, and piped the match to a single file. I have a 96-core machine so that each grep job was sent to the background to speed up the search: file1.tab chr1A_part1 123241847 123241848... (6 Replies)
Discussion started by: yifangt
6 Replies

2. UNIX for Beginners Questions & Answers

Grep file starting from pattern matching line

I have a file with a list of references towards the end and want to apply a grep for some string. text .... @unnumbered References @sp 1 @paragraphindent 0 2017. @strong{Chalenski, D.A.}; Wang, K.; Tatanova, Maria; Lopez, Jorge L.; Hatchell, P.; Dutta, P.; @strong{Small airgun... (1 Reply)
Discussion started by: kristinu
1 Replies

3. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

4. Shell Programming and Scripting

Grep log file to get line above matching pattern

Hi, I have a log file that looks like this "delete" : { "_type" : "cl", "_id" : "1000600000000562636", "_version" : 1, "status" : 200, "found" : false } }, { "delete" : { "_type" : "cl", "_id" : "1000600000000562643", ... (4 Replies)
Discussion started by: wahi80
4 Replies

5. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

6. Linux

matching two fields

Hi I am having 2 fields and if f1=f2 i wanna print that line eg 1 2 1 3 1 9 2 2 3 5 9 9 In the abov eg. the highlighted lines shud be printed 2 2 9 9 Thanking u (3 Replies)
Discussion started by: binnybio
3 Replies

7. Shell Programming and Scripting

find out line number of matching string using grep

Hi all, I want to display line number for matching string in a file. can anyone please help me. I used grep -n "ABC" file so it displays 6 ABC. But i only want to have line number,i don't want that it should prefix matching context with line number. Actually my original... (10 Replies)
Discussion started by: sarbjit
10 Replies

8. Shell Programming and Scripting

Matching by key fields

I have a file (key.dat) that contains two columns: AA|1234| BB|567| CC|8910| I have another file (extract.dat) that contains some data: SD|458|John|Smith| AA|3345|Frank|Williams| AA|1234|Bill|Garner| BD|0098|Yu|Lin| BB|567|Gail|Hansen| CC|8910|Ken|Nielsen| I want to compare the... (5 Replies)
Discussion started by: ChicagoBlues
5 Replies

9. Shell Programming and Scripting

matching 2 exact fields

Dear experts, I have a file1 that looks like 60127930928 2091 60129382039 2092 60126382937 2091 60128937928 2061 60127329389 2062 60123748730 2061 60128730293 2061 and file 2 that looks like 60127930928 2091 60129382039 2092 60126382937 2093 60128937928 2061 60127329389... (2 Replies)
Discussion started by: aismann
2 Replies

10. UNIX for Dummies Questions & Answers

How to grep / zgrep to output ONLY the matching filename and line number?

Hi all, I am trying to zgrep / grep list of files so that it displays only the matching filename:line number and does not display the whole line, like: (echo "1.txt";echo "2.txt") | xargs zgrep -no STRING If I use -o option, it displays the matching STRING and if not used, displays the... (3 Replies)
Discussion started by: vvaidyan
3 Replies
Login or Register to Ask a Question