Compare the second column of a file with the second column of another in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare the second column of a file with the second column of another in awk
# 1  
Old 05-23-2013
Compare the second column of a file with the second column of another in awk

Hi,

I know that this topic has been discussed in the past and I've tried to follow all the guidelines. Anyhow, I following describe my problem.

I have a file (file1 , no. records = 67) containing pairs of IP addresses as follows (with single space as delimiter between the fields):

example of file1
Code:
1.2.3.4 123.45.2.1
1.2.3.4 68.32.1.4
........  ............

and another file (file2) which is much larger in size (no. records = 500 000) that the first two fields are still IP addresses (single space as delimiter in all fields) that they do also provide some other information in the subsequent fields (48 fields on every record/line)...as follows

example of file2
Code:
1.2.3.4 123.45.2.1 53 4000 2.5 30 21 43 .....
1.2.3.4 68.32.1.4   500 80 3.7 40 22 10 .....
1.2.3.4 50.44.66.1 40 30 8.5 29 11 10 .....

Since the first field is the same, I want to compare the second field of file1 with the second field of file2 and if matched to print the whole record (with all the 48 fields) from file2 into a new file (i.e. file3)

I've tried to do with the following one-liner

Code:
awk 'NR==FNR{a[$2]=$0;next}a[$2]{$0=a[$2]}1'  file2 file1 > file3

which provides me a result but it only gives me 67 records in the newly created file3...by going visually through file2 I could see more matches...any suggestions?

Thanks in advance!
# 2  
Old 05-23-2013
Try this:
Code:
awk 'NR==FNR{A[$2];next}$2 in A' file1 file2

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-23-2013
thanks Yoda! It worked...now found 277 new records! Anyhow, just out of curiosity, what was the reason I was obtaining only 67 instances from the previous code? It would be great if you can enlighten me on that.

Thanks again and may the force be with you Smilie
# 4  
Old 05-23-2013
The code that you posted was actually comparing 2nd field in file2 with 2nd field in file1:
Code:
awk 'NR==FNR{a[$2]=$0;next}a[$2]{$0=a[$2]}1'  file2 file1

But your requirement was to compare it other way! I believe this must have caused the discrepancy in the output because you said file1 contains 67 records.

Also your code was logically wrong because you put a 1 to print each record regardless the record is found or not:
Code:
awk 'NR==FNR{a[$2]=$0;next}a[$2]{$0=a[$2]}1'  file2 file1

I hope this helps.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-23-2013
this helped a lot, thank you again!
# 6  
Old 05-23-2013
You said field1 is the same in both files, so you could include it in the search as well. grep could do it as well, but might be overwhelmed when it comes to really large files:
Code:
$ grep -Ff file1 file2
1.2.3.4 123.45.2.1 53 4000 2.5 30 21 43 .....
1.2.3.4 68.32.1.4   500 80 3.7 40 22 10 .....

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to compare 3rd column value with first column and display

Hello Team, My source data (INput) is like below EPIC1 router EPIC2 Targetdefinition Exp1 Expres rtr1 Router SQL SrcQual Exp1 Expres rtr1 Router EPIC1 Targetdefinition My output like SQL SrcQual Exp1 Expres Exp1 Expres rtr1 Router rtr1 Router EPIC1 Targetdefinition... (5 Replies)
Discussion started by: sekhar.lsb
5 Replies

2. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

3. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

4. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

5. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

6. Shell Programming and Scripting

How to compare the values of a column in a same file using awk?

Dear Unix experts, I have got a file where I would like to compare the values of second column if first column is same in such a way that the difference between the values is >50. If not, I would like to discard both values. For example, my input file looks like - comp275_c0_seq2 73... (7 Replies)
Discussion started by: utritala
7 Replies

7. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

8. Shell Programming and Scripting

awk compare column n replace with in one file

hi Friends need to compare columns in one file where the data looks like below laptop,IBM phone,samsung car,rental user1,laptop user2,laptop user3,phone want to get output as laptop,IBM phone,samsung car,rental user1,IBM user2,IBM user3,samsung need to seach $2 in array of $1 and... (4 Replies)
Discussion started by: arun1401
4 Replies

9. Shell Programming and Scripting

How to compare the values of a column in awk in a same file and consecutive lines..

I would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line. Input File ========== PDB 2500 RTDB 123 RTDB-EAGLE 122 VSCCP 2565... (4 Replies)
Discussion started by: manuswami
4 Replies
Login or Register to Ask a Question