Govins
Govins

Reputation: 41

how to compare 2 files column of a linux file to be compared with second column of another file and get the difference

How to compare 2 files? I need to compare a column of a linux file with the second column of another file and get the difference.

Let's say I have the following files.

file 1:

a 3
b 6
c 8
d 7
g 5
p 16

file 2:

a 1
b 6
c 8
d 7
g 5

I need to compare column two of file 1 with column two of file 2 and get the difference.

Desired output file 1 - file 2 :

a 2
b 0
c 0
d 0
g 0
p 16

Upvotes: 0

Views: 44

Answers (1)

Kent
Kent

Reputation: 195269

This awk one-liner works for your example:

awk 'NR==FNR{a[$1]=$2;next}{print $1,a[$1]-$2;delete a[$1]}
       END{for(x in a)print x, a[x]}' file1 file2

Upvotes: 1

Related Questions