Comparing files line by line in awk with delimiter Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Comparing files line by line in awk with delimiter without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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
We are here to answer your question about Comparing files line by line in awk with delimiter - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji