LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-14-2007, 09:53 PM   #1
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 540

Rep: Reputation: 30
How do I compare two values in separate files


Hello all,

I have two files, one like this:

abc123
abc124
abc127
ABC128

and the second like this:

ABC123
abc125
abc126
abc128

What i want to do is somehow find out which rows are unique to each file (but ignore the case).

Ideally, the second row in the first file would be changed to this:
abc123 - unique

My files don't have the values above, those are just to show you the idea. Each set of files (I have many I need to compare) has something in the 500 to 600 line/values range.

-Thanks for the help, and I hope my question makes sense
 
Old 09-14-2007, 10:27 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
You want the diff command. This is what it was made for.
 
Old 09-14-2007, 10:33 PM   #3
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
diff -ui file1 file2
 
Old 09-14-2007, 11:56 PM   #4
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 540

Original Poster
Rep: Reputation: 30
That worked like a charm.

-Thank you very much!
 
Old 09-15-2007, 12:34 AM   #5
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 540

Original Poster
Rep: Reputation: 30
Guess what...I spoke too soon!

The results totally confuse me. Maybe someone can help.

I will post some of the real files so you can see what i mean

file 1:
dv1000
dv1000 cto
dv1000-dz678av
dv1000-dz731av
dv1000t
dv1000t cto
dv1001ap-pk809as
dv1001xx-pk807as
dv1002ap-pf352pa


file2:
dv1000 cto
dv1000
dv1000t cto
dv1001ap
dv1001xx
dv1002ap
dv1002xx
dv1003ap

My results:
-dv1000
dv1000 cto
-dv1000-dz678av
-dv1000-dz731av
-dv1000t
+dv1000
dv1000t cto
-dv1001ap-pk809as
-dv1001xx-pk807as
-dv1002ap-pf352pa
+dv1001ap
+dv1001xx
+dv1002ap
+dv1002xx
+dv1003ap

What i wanted was the script to tell me that each file already has dv1000 in it, So why is there a - and a + entry for the dv1000 in the results file? Here is what i ran:

diff -ui pavilion1.txt pavilion2.txt > diff.txt

Where did I go wrong?
 
Old 09-15-2007, 02:34 AM   #6
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
diff -ui fileone filetwo | grep -v [-+] >outputfile

This will output any shared entries. No output = no shared items.

As for the - and + returned by the command you used, that's how differences are indicated: - means present in file 1 only, + means present in file two only. Neither - nor + means that the item occurs in both files.

Last edited by jay73; 09-15-2007 at 02:36 AM.
 
Old 09-15-2007, 02:44 AM   #7
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
In fact, now that I come to think of it, this may be safer:

diff -ui fileone filetwo | grep -v "^[-+].*"
 
Old 09-15-2007, 12:41 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Try using the -y flag in diff. That will print out contents of both files in columns so you can easily compare them and see which lines are different. Might not be very readable if the individual lines are very long though.

There are lots of other flags you can use for different styles of output. Read the man file.

Last edited by David the H.; 09-15-2007 at 12:44 PM.
 
Old 09-19-2007, 04:55 PM   #9
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 540

Original Poster
Rep: Reputation: 30
ok, i finally got it working. I had to do

diff -uia --ignore-all-space fileone filetwo


That got it working for some reason. I just randomly tried the ignore all space dash, even thought there are no spaces in either of the files, but it did work.

That's linux commands for you in my experience, sometimes they just don't make sense, but you can still get the job done. Gotta love it. Would have been way more trouble in windows though, so i can't complain.

-Thanks for the help everyone.
 
Old 09-19-2007, 08:49 PM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Actually, there are spaces in your example files ... eg 'dv1000 cto'
 
Old 09-19-2007, 09:36 PM   #11
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 540

Original Poster
Rep: Reputation: 30
You're right on that one, but my main concern was why did the dv1000 show up with a - and a +. It should have had neither, and appeared only once (indicating that it was in both files).
 
Old 09-19-2007, 11:36 PM   #12
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
Diff also checks whether items occupy the same lines in both files. Your items didn't.
 
  


Reply


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
Encoding separate audio channels to separate files omnio Linux - Software 0 06-01-2007 07:46 AM
Compare files in specific directory to files on CD szim90 Linux - Newbie 3 02-10-2007 08:39 PM
Looking for a tool to auto crop and separate images in to separate files.. mlsfit Linux - Software 2 08-06-2006 03:13 PM
Compare two files namit Linux - Software 1 12-31-2005 08:10 AM
How do I compare 2 files? linuxhippy Slackware 6 03-26-2005 01:54 AM


All times are GMT -5. The time now is 01:25 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration