How to compare two files using four fields and print out the mismatched fields ?
Hello experts,
I have two files in different sizes but the same 4 column data format.
And I would like to compare each line.
If the first 3 fields are matched but not the fourth
print out the whole line of 1 file and add the fourth
mismatched field from the other file to the end.
Here is an example,
file1,
switch1 Ethernet6 8 2500
switch1 Ethernet6 7 2513
switch1 Ethernet4 35 1
switch2 Ethernet4 36 4
switch2 Ethernet5 1 2660
switch2 Ethernet6 4 5
file2,
switch2 Ethernet5 1 2691
switch1 Ethernet6 7 2513
switch1 Ethernet6 8 2587
switch1 Ethernet6 34 1
switch2 Ethernet5 1 2660
switch2 Ethernet4 36 4
switch2 Ethernet6 4 7
switch2 Ethernet5 12 4
The results should be
switch1 Ethernet6 8 2500 2587
switch2 Ethernet6 4 5 7
I have tried with grep, comm, awk with arrays, but so far I didn't get the expected results yet.
I learned from one of the experts that grep -v -f will display the differences, but I also need the fourth mismatched field at the end of the output line.
The second scenario is to compare two files, and if
the first two fields are matched but not the third and the fourth, then print the whole line of either file
with the 2 mismatched (third and fourth) fields from the other file at the end.
Any help is much appreciated.
Thanks
|