Reputation: 19
I am trying to compare to files on an Ubuntu system. Both files have the same two column structure, and if the value of column two match for a given line subtract the values of column 1 for the same line.
I have tried a couple of things, the closest I got is with this command :
awk 'NR==FNR{a[$1]=$1;b[$2]=$2;next} {if ($2 in b) print $1-a[$1]," ",$2}' file1 file 2
But I guess I have something wrong since the result seems to acknowledge only lines that fill the condition where the second column match but allows only lines that have the same value for the first column.
For example:
File1 :
10 A
20 B
30 C
10 D
File2 :
10 A
20 E
20 F
50 D
Should return :
0 A
-40 D
Returns :
0 A
Upvotes: 1
Views: 243
Reputation: 203229
$ awk 'NR==FNR{a[$2]=$1; next} $2 in a{print a[$2]-$1, $2}' file1 file2
0 A
-40 D
Upvotes: 2