Reputation: 31
I have two files name as 1) File-New 2) File-Old. they contains data as shown below,
case 1
File-New
7! J9AA-104445-A-BOM-50! REINF RR KIT FLR S/M LH! 35!
8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 22!
7! JWZZ-102225-A-MOM-50! RZFIF RR KRT FLR W/Z LH! 15!
File-Old
7! J9AA-104445-A-BOM-50! REINF RR KIT FLR S/M LH! 34!
8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 21!
7! JWZZ-102225-A-MOM-50! RZFIF RR KRT FLR W/Z LH! 14!
When we run awk -F"!" 'NR==FNR{++a[$2,$4];next} !(a[$2,$4])'
logic for both files Expected output is,
$ /usr/xpg4/bin/awk -F"!" 'NR==FNR{++a[$2,$4];next}
!(a[$2,$4])' "File-New" "File-Old"
7! J9AA-104445-A-BOM-50! REINF RR KIT FLR S/M LH! 34!
8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 21!
7! JWZZ-102225-A-MOM-50! RZFIF RR KRT FLR W/Z LH! 14!
From output we can see the logic of command -F"!" 'NR==FNR{++a[$2,$4];next} !(a[$2,$4])'
is written to print the difference in "File-Old". But above logic fails if the data of both files changed as shown below,
Case 2
File-New
7! J9AA-104445-A-BOM-50! REINF RR KIT FLR S/M LH! 35!
8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 22!
7! JWZZ-102225-A-MOM-50! RZFIF RR KRT FLR W/Z LH! 15!
7! J9AA-104445-A-BOM-50! REINF RR KIT FLR S/M LH! 34!
8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 21!
File-Old
7! J9AA-104445-A-BOM-50! REINF RR KIT FLR S/M LH! 34!
8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 21!
7! JWZZ-102225-A-MOM-50! RZFIF RR KRT FLR W/Z LH! 14!
7! J9AA-104445-A-BOM-50! REINF RR KIT FLR S/M LH! 34!
8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 21!
Run the same logic for case 2
$ /usr/xpg4/bin/awk -F"!" 'NR==FNR{++a[$2,$4];next}
!(a[$2,$4])' "File-New" "File-Old"
7! JWZZ-102225-A-MOM-50! RZFIF RR KRT FLR W/Z LH! 14!
Expected Out is
7! J9AA-104445-A-BOM-50! REINF RR KIT FLR S/M LH! 34!
8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 21!
7! JWZZ-102225-A-MOM-50! RZFIF RR KRT FLR W/Z LH! 14!
Is someone able to suggest what I need to change in logic -F"!" 'NR==FNR{++a[$2,$4];next} !(a[$2,$4])' to get the Expected output for case 2 without changing the file order "File-New" "File-Old".
Note : we have to keep the Order of file passing to command is Fix i.e "File-New" "File-Old"
Upvotes: 0
Views: 85
Reputation: 632
The awk
snippet you are using constructs a dictionary with keys equal to values from second and fourth field (with "!" separated fields). It fails to do what you expect because for example for this line (2nd line in "File-new"):
8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 22!
Which changes to this line
8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 21!
There is also another line in "File-old" with identical 2nd and 4th fields. So the value at key [J9BB-103365-A-BOM-50,21]
of array a
is not zero, thus the line is not printed. If you wish to monitor changes to each line on these 2 fields, you must also store the line number in your array, yielding this:
awk -F'!' 'NR==FNR{++a[$2,$4,FNR];next}
!(a[$2,$4,FNR])' "File-New" "File-Old"
Maybe you will wish to complete this by also printing the line number:
awk -F'!' 'NR==FNR{++a[$2,$4,FNR];next}
!(a[$2,$4,FNR]){printf "Line %d changed: %s\n", FNR, $0}' "File-New" "File-Old"
Output:
Line 1 changed: 7! J9AA-104445-A-BOM-50! REINF RR KIT FLR S/M LH! 34!
Line 2 changed: 8! J9BB-103365-A-BOM-50! MBR REINF FLR SD LH! 21!
Line 3 changed: 7! JWZZ-102225-A-MOM-50! RZFIF RR KRT FLR W/Z LH! 14!
Upvotes: 1