LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-21-2014, 09:09 PM   #1
Polarbearcub
LQ Newbie
 
Registered: Apr 2014
Posts: 2

Rep: Reputation: Disabled
Red face Help with scripting


Hi All, I need to find differences in two files.

File1
name joe
num 100
name john
num 5460
name smith
num 561
name George
num 7558

File2
name smith
num 561
name George
num 7558
name joe
num 99
name alice
num 0

I have 4000 lines in File1 and 3996 in File2
I need to find the 4 missing lines and make sure the others match up. In my example you see that joe in file1 has 100 but in file2 it's 99. I need to know the differences and the 4 missing lines only.

Thank you for any help!

Last edited by Polarbearcub; 04-21-2014 at 09:13 PM.
 
Old 04-21-2014, 09:30 PM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
use can use the 'diff' command to find the difference between two files. sometimes it's helpful to sort each file with the 'sort' command before comparing them.
 
Old 04-21-2014, 09:46 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
May we trust that every name line is followed by a num line?

In other words, may we change this ...
Code:
name joe
num 100
name john
num 5460
name smith
num 561
name George
num 7558
... into this?
Code:
name joe num 100
name john num 5460
name smith num 561
name George num 7558
Daniel B. Martin
 
Old 04-21-2014, 09:52 PM   #4
Polarbearcub
LQ Newbie
 
Registered: Apr 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Yes, we can trust this. Each name has a number on the next line.
 
Old 04-21-2014, 10:29 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Polarbearcub View Post
Yes, we can trust this. Each name has a number on the next line.
With InFile1 ...
Code:
name joe
num 100
name john
num 5460
name smith
num 561
name George
num 7558
... and InFile2 ...
Code:
name smith
num 561
name George
num 7558
name joe
num 99
name alice
num 0
... this code ...
Code:
echo; echo "Create Name/Num pairs and sort them."
paste -sd " \n"  $InFile1 |sort >$Work1
paste -sd " \n"  $InFile2 |sort >$Work2

echo "Name/Num pairs found in both input files ..." >$OutFile1
comm -12 $Work1 $Work2 >>$OutFile1
echo; cat $OutFile1

echo "Name/Num pairs found only in InFile1 ..." >$OutFile2
comm -23 $Work1 $Work2 >>$OutFile2
echo; cat $OutFile2

echo "Name/Num pairs found only in InFile2 ..." >$OutFile3
comm -13 $Work1 $Work2 >>$OutFile3
echo; cat $OutFile3
... produced this result ...
Code:
Name/Num pairs found in both input files ...
name George num 7558
name smith num 561

Name/Num pairs found only in InFile1 ...
name joe num 100
name john num 5460

Name/Num pairs found only in InFile2 ...
name alice num 0
name joe num 99
Daniel B. Martin
 
1 members found this post helpful.
Old 04-22-2014, 01:56 PM   #6
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
danielbmartin, How have I been using Linux for over two decades and never heard of 'paste' and 'comm'?

Thanks for posting this solution.
 
Old 04-22-2014, 02:54 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by sharky View Post
How have I been using Linux for over two decades and never heard of 'paste' and 'comm'?
I'll offer a speculative answer.

The Linux world seems to be roughly divided into two segments:
- system administrators
- applications programmers

Are you an administrator? Admins learn and use a certain subset of Linux commands. Apps guys use a different subset. There is some overlap, of course.

My Linux programming is purely recreational and entirely apps. I'm retired and learning Linux (self-taught) for fun and to stave off old-age brain rot.

If you learned something new today, that's good. I try to learn something new every day and don't always succeed.

Daniel B. Martin

Last edited by danielbmartin; 04-22-2014 at 03:06 PM. Reason: Elaboration and clarification
 
Old 04-29-2014, 07:11 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Code:
fgrep -vxf smallfile bigfile
works with unsorted files.
 
  


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
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 04:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 10:09 PM
Need scripting help!! stan0934 Programming 5 05-29-2006 01:29 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
scripting help please deepsix Programming 10 09-08-2005 07:49 PM

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

All times are GMT -5. The time now is 06:43 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