LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to compare two files using four fields and print out the mismatched fields ? (https://www.linuxquestions.org/questions/programming-9/how-to-compare-two-files-using-four-fields-and-print-out-the-mismatched-fields-4175503721/)

avatar007 05-02-2014 12:17 PM

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

schneidz 05-02-2014 12:20 PM

is this a dupe ?:
http://www.linuxquestions.org/questi...9/#post5162171

avatar007 05-02-2014 12:31 PM

Yes and no.
I posted that thread, but now my report need an extra field and the search scenario is slightly different. I thought about using that same thread, but some may say it's a different question now.
I can post the new question on that old thread and delete this one.
Please let me know as I'm new here.

Thanks

Mara 05-04-2014 11:35 AM

In this case you should probably update this thread. But, you already gave an answer in the other thread, so I'm closing this one so all the further replies go to the other one.

So, pleaee continue the discussion in http://www.linuxquestions.org/questi...9/#post5162171


All times are GMT -5. The time now is 08:04 AM.