LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-02-2014, 12:17 PM   #1
avatar007
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,

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
 
Old 05-02-2014, 12:20 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
is this a dupe ?:
http://www.linuxquestions.org/questi...9/#post5162171
 
Old 05-02-2014, 12:31 PM   #3
avatar007
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.

Thanks
 
Old 05-04-2014, 11:35 AM   #4
Mara
Moderator
 
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 http://www.linuxquestions.org/questi...9/#post5162171
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 PM.

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