Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-02-2014, 12:17 PM   #1
LQ Newbie
Registered: Dec 2013
Posts: 5

Rep: Reputation: Disabled
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,


switch1 Ethernet6 8 2500
switch1 Ethernet6 7 2513
switch1 Ethernet4 35 1
switch2 Ethernet4 36 4
switch2 Ethernet5 1 2660
switch2 Ethernet6 4 5


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.

Old 05-02-2014, 12:20 PM   #2
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,258

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
is this a dupe ?:
Old 05-02-2014, 12:31 PM   #3
LQ Newbie
Registered: Dec 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.

Old 05-04-2014, 11:35 AM   #4
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to compare two files using four fields each line and print out the mismatch ? avatar007 Programming 2 04-30-2014 04:35 PM
[SOLVED] deleting fields in lines that have more fields than the average patolfo Linux - Software 4 09-14-2011 11:03 AM
compare files with fields separated with ':' Alkass Programming 3 07-18-2011 03:54 AM
[SOLVED] Compare two fields, then sort based on result. C.L. Programming 7 09-28-2010 09:43 AM
Compare two fields on consecutive rows and print the two rows aditi_borkar Linux - Newbie 3 04-09-2009 05:49 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration