VDC
VDC

Reputation: 1449

Matching numbers in two different files using awk

I have two files (f1 and f2), both made of three columns, of different lengths. I would like to create a new file of four columns in the following way:

f1             f2
1 2 0.2        1 4 0.3
1 3 0.5        1 5 0.2
1 4 0.2        2 3 0.6
2 2 0.5        
2 3 0.9

If the numbers in the first two columns are present in both files, then we print the first two numbers and the third number of each file (e.g. in both there is 1 4, in f3 there should be 1 4 0.2 0.3; otherwise, if the two first numbers are missing in f2 just print a zero in the fourth column.

The complete results of these example should be

f3
1 2 0.2 0
1 3 0.5 0
1 4 0.2 0.3
2 2 0.5 0
2 3 0.9 0.6

The script that I wrote is the following:

awk '{str1=$1; str2=$2; str3=$3; 
     getline < "f2"; 
     if($1==str1 && $2==str2)
        print str1,str2,str3,$3 > "f3";
     else
        print str1,str2,str3,0 > "f3";
}' f1

but it just looks if the same two numbers are in the same row (it does not go through all the f2 file) giving as results

1 2 0.2 0
1 3 0.5 0
1 4 0.2 0
2 2 0.5 0
2 3 0.9 0

Upvotes: 2

Views: 126

Answers (1)

anubhava
anubhava

Reputation: 785276

This awk should work:

awk 'FNR==NR{a[$1,$2]=$3;next} {print $0, (a[$1,$2])? a[$1,$2]:0}' f2 f1
1 2 0.2 0
1 3 0.5 0
1 4 0.2 0.3
2 2 0.5 0
2 3 0.9 0.6

Upvotes: 1

Related Questions