True Entertainer
True Entertainer

Reputation: 57

Compare 2 files with multiple columns | shell

I have a 2 files fil1.txt and fil2.txt

My requirement is to compare column1 and column3 of both the files and if it matches then check the column2 values and if column2 value is less than equal to column2 value of second file than remove it from the list and print rest entries.

example below-

fil1.txt has content

abc 25 bcd
xyz 30 efg
pqr 25 qrs

fil2.txt has content

abc 30 bcd
xyz 30 efg
rst 25 uvw
pqs 35 lmn

My output should be

pqr 25 qrs

Below code is working but not sure how I can compare column2 value.

awk 'FNR==NR{a[$1,$3];next} !(($1,$3) in a)' fil1.txt fil2.txt

Upvotes: 0

Views: 91

Answers (1)

anubhava
anubhava

Reputation: 786289

You may use this awk:

awk 'FNR == NR {a[$1,$3] = $2; next} $2 > a[$1,$3]' file2 file1

pqr 25 qrs

Upvotes: 2

Related Questions