Reputation: 11
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
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
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
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