user2804482
user2804482

Reputation: 11

awk script: check if all words(fields) from one file are contained in another file

I am new to awk scripting. I want to do a field by word (field) comparison of two files File1.txt and File2.txt. The files contain a list of | (pipe) separated field.

    File 1:
    -------------------
    aaa|bbb|ccc|eee|fff
    lll|mmm|nnn|ooo|ppp
    rrr|sss|ttt|uuu|vvv

    File 2: 
    -------------------
    aaa|bbb|ccc|eee|fff
    rrr|sss|ttt|uuu|vvv
    rrr|sss|ttt|uuu|uuu

We compare the same line no. in both the files.

Fields in Line 1 of both file match.

In Line 2 all the fields (lll, mmm, nnn, ooo, ppp) donot not match with all fields (rrr, sss, ttt, uuu, vvv) in line 2 of File 2. Similarly the 5th field (vvv, uuu) of 3rd line in both the files donot match.

Hence Line no. 2 and Line no. 3 should get echoed by bash.

Both files will follow an order.

Upvotes: 1

Views: 1401

Answers (3)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

The following lines may be adapted following needs, another language like perl may be more appropriate

i=1
while read -r -u4 l1 || read -r -u5 l2; do
  if [[ $l1 != $l2 ]]; then
    echo "$i: $l1 != $l2"
  fi
  ((i+=1))
done 4<file1 5<file2

Upvotes: 0

user1502952
user1502952

Reputation: 1420

Two compare two files, better use already inbuilt command sdiff:

 sdiff File1 File2

This will display the lines which differ in both files.

Doing with awk.

awk -F '|' 'NR==FNR{a[$0];next}!($0 in a){print $0}' file1 file2

Upvotes: 1

Kent
Kent

Reputation: 195039

this line should do:

awk 'NR==FNR{a[FNR]=$0;next}a[FNR]!=$0' file1 file2

output:

rrr|sss|ttt|uuu|vvv
rrr|sss|ttt|uuu|uuu

Upvotes: 2

Related Questions