awk compare 2 columns, 2 files, output whole line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk compare 2 columns, 2 files, output whole line
# 1  
Old 10-03-2011
awk compare 2 columns, 2 files, output whole line

Hello,

I have not been able to find what I'm looking for via searching the forum. I could use some help with an awk script or one-liner to solve this simple problem.

I have two files. If $1 and $2 from file1 match $1 and $2 from file2, print the whole line from file2.

Example file1

Code:
-47.2 -19.0 18.12 13.78 0.36 0.56 0.001 ABPO
-38.8 9.0 24.02 18.91 0.41 0.48 0.001 ADIS
-0.5 38.3 19.06 16.84 0.29 0.12 0.003 ALAC

Example file2

Code:
-38.8 9.0 -1.78172 11.3782 0.01 0.01 0.01 31704
-0.5 38.3 -1.83964 11.2841 0.01 0.01 0.01 31701
-2.3 37.6 -1.82152 11.3152 0.01 0.01 0.01 31702
-47.2 -19.0 -1.80205 11.3466 0.01 0.01 0.01 31703
-2 37.6 -1.76099 11.41 0.01 0.01 0.01 31705
29.9 31.2 -1.7402 11.4417 0.01 0.01 0.01 31706

Output should be:

Code:
-47.2 -19.0 -1.80205 11.3466 0.01 0.01 0.01 31703
-38.8 9.0 -1.78172 11.3782 0.01 0.01 0.01 31704
-0.5 38.3 -1.83964 11.2841 0.01 0.01 0.01 31701


Thanks so much for your help.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-03-2011 at 03:22 PM.. Reason: code tags, please
# 2  
Old 10-03-2011
Code:
nawk '{idx=$1 $2} FNR==NR{f1[idx];next} idx in f1' file1 file2

These 2 Users Gave Thanks to vgersh99 For This Post:
# 3  
Old 10-04-2011
Code:
awk 'FNR==NR{A[$1,$2]=1;next} A[$1,$2]' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 columns of files awk

hello everybody I have 2 files the file1 has 10 columns and the form: ... 110103 0802 1.16 38 20.16 22 1.21 8.77 0.00 20 120103 0832 23.40 38 22.10 21 46.35 10.17 0.00 28 120103 1413 45.00 38 24.50 21 48.85 7.89 0.00 38 130103 1112 23.40 38 22.10 21 48.85 ... (5 Replies)
Discussion started by: phaethon
5 Replies

2. Shell Programming and Scripting

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

3. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

4. Shell Programming and Scripting

Compare columns in two different files using awk

Hi, I want to compare the columns of two files excluding column 2 from both the files. I tried this awk command. awk -F":" 'NR==FNR{++a;next} !(a)' file1.txt file2.txt . Example: File1.txt 123:09-15-2011:abc:123456 123:09-15-2011:abc:234567 123:09-15-2011:abc:345678 ... (5 Replies)
Discussion started by: shell_newbie
5 Replies

5. Shell Programming and Scripting

Compare intervals (columns) from two files (awk, grep, Perl?)

Hi dear users, I need to compare numeric columns in two files. These files have the following structure. K.txt (4 columns) A001 chr21 9805831 9846011 A002 chr21 9806202 9846263 A003 chr21 9887188 9988593 A003 chr21 9887188 ... (2 Replies)
Discussion started by: jcvivar
2 Replies

6. Shell Programming and Scripting

awk compare specific columns from 2 files, print new file

Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3. FILE1 1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04 1467 55.27433... (1 Reply)
Discussion started by: jm4smtddd
1 Replies

7. Shell Programming and Scripting

Compare two files and output difference, by first field using awk.

It seems like a common task, but I haven't been able to find the solution. vitallog.txt 1310,John,Hancock 13211,Steven,Mills 122,Jane,Doe 138,Thoms,Doe 1500,Micheal,May vitalinfo.txt 12122,Jane,Thomas 122,Janes,Does 123,Paul,Kite **OUTPUT** vitalfiltered.txt 12122,Jane,Thomas... (2 Replies)
Discussion started by: charles33
2 Replies

8. Shell Programming and Scripting

AWK Compare files, different fields, output

Hi All, Looking for a quick AWK script to output some differences between two files. FILE1 device1 1.1.1.1 PINGS device1 2.2.2.2 PINGS FILE2 2862 SITE1 device1-prod 1.1.1.1 icmp - 0 ... (4 Replies)
Discussion started by: stacky69
4 Replies

9. Shell Programming and Scripting

Compare selected columns of two files and print whole line with mismatch

hi! i researched about comparing two columns here and got an answer. but after examining my two files, i found out that the first columns of the two files are not unique with each other. all i want to compare is the 2nd and 3rd column. FILE 1: ABS 456 315 EBS 923 163 JYQ3 654 237 FILE 2:... (1 Reply)
Discussion started by: engr.jay
1 Replies

10. Shell Programming and Scripting

compare columns from seven files and print the output

Hi guys, I need some help to come out with a solution . I have seven such files but I am showing only three for convenience. filea a5 20 a8 16 fileb a3 42 a7 14 filec a5 23 a3 07 The output file shoud contain the data in table form showing first field of... (7 Replies)
Discussion started by: smriti_shridhar
7 Replies
Login or Register to Ask a Question