Comparing files line by line in awk with delimiter

file1:

abc|123|check
def|456|map
ijk|789|globe
lmn|101112|equator

file2:

abc|123|check
def|456|map
ijk|789|equator
lmn|101112|globe

EXPECTED OUTPUT:

ijk|789|equator
lmn|101112|globe

Current awk script:

awk 'BEGIN{OFS=FS="|"} NR==FNR{a[$3]=$3;next}!($3 in a)' file1 file2

This does comparison based on array content. How to compare line by line and printing only that results.

Answer

If I understand you correctly, you want to print a line from file2 if the 3rd field is different to the corresponding entry in file1. If so, this should do it:

awk 'BEGIN{FS="|"} NR==FNR{a[$1,$2]=$3;next}(a[$1,$2]!=$3)' file1  file2

Yours wasn’t working because you were taking $3 as the key for array a and $3 is not unique (both equator and globe are present in both files).

I agree with @drewbenn that both grep and join are simpler for this particular case, but here’s a Perl way of doing the same thing:

perl -laF'|' -ne '($k{$F[0].",".$F[1]}||=$F[2]) eq $F[2]||print;' file1  file2

Leave a Reply

Your email address will not be published. Required fields are marked *