Show the Difference between two files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Show the Difference between two files

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-11-2007
The One The One is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 20 December 2012, 4:36 AM EST
Posts: 46
Thanks: 4
Thanked 0 Times in 0 Posts
Show the Difference between two files

I have two files and I need to know the difference between each line. This will extend to thousand lines and manual works is really not really an option.
sample:

First File Second File
allan entry1 entry2 entry3 allan entry1 entry3
bob entry1 entry2 entry3 entry4 bob entry1 entry4

I want to output the difference only. Sample output:

allan entry2
bob entry2 entry3

I know that simple grep will not work here. UNIX is new to me and this is the only way i think this can be done.
Sponsored Links
    #2  
Old 03-11-2007
dennis.jacob's Avatar
dennis.jacob dennis.jacob is offline Forum Advisor  
Registered User
 
Join Date: Feb 2007
Last Activity: 30 March 2014, 9:33 AM EDT
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 12 Times in 11 Posts
Hi,

First, I am not very clear about the way you explained the content of two files and the sample output.Very sorry abt that.

But you can do file comparison using "comm" or "diff" or "cmp" commands...

Thnx
Dennis
Sponsored Links
    #3  
Old 03-11-2007
The One The One is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 20 December 2012, 4:36 AM EST
Posts: 46
Thanks: 4
Thanked 0 Times in 0 Posts
Sorry about that. These are the files.

First File
allan entry1 entry2 entry3
bob entry1 entry2 entry3 entry4

Second File
allan entry1 entry3
bob entry1 entry4

Output:
allan entry2
bob entry2 entry3

Using diff and comm doesnt give me the result that I want. The result should only be the name and the the entry that cant be found on the second file. In my example, allan will have entry 2 since it is not on the second file.
    #4  
Old 03-12-2007
Perderabo's Avatar
Perderabo Perderabo is online now Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 19 September 2014, 1:30 PM EDT
Location: Ashburn, Virginia
Posts: 9,925
Thanks: 60
Thanked 435 Times in 260 Posts
Are the two files guaranteed to have the same number of lines? So if a bob line is in one file it is also in the other? Are the records in the same sequence in both files?
Sponsored Links
    #5  
Old 03-12-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts
If you have Python and know the language, here's an alternative:

Code:
#!/usr/bin/python
for line in open("file2"):
    line = line.strip() #get rid of newlines
    name,entry = line.split(' ',1)
    for lin in open("file1"):
        lin = lin.strip()
        if lin.startswith(name):
                for e in entry.split():
                    lin = lin.replace(e , "")
                print "Output: ", lin

output:

Code:
/test # ./test.py
Output:  allan  entry2
Output:  bob  entry2 entry3

Sponsored Links
    #6  
Old 03-12-2007
The One The One is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 20 December 2012, 4:36 AM EST
Posts: 46
Thanks: 4
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Perderabo
Are the two files guaranteed to have the same number of lines? So if a bob line is in one file it is also in the other? Are the records in the same sequence in both files?

Yes, they have same number of lines and also on same sequece.

I dont have a python so I cant use the code given by ghostdog74.
Sponsored Links
    #7  
Old 03-12-2007
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 17 September 2014, 3:10 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts

Code:
awk ' BEGIN { while ( getline < "first_file" ) { arr[$1]=$0; } }
{ for( i = 2 ; i <= NF ; ++i ) 
	sub($i,"",arr[$1])  
  gsub("  +"," ",arr[$1])
  print arr[$1] 
} ' second_file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare 2 files and show count same content. ooilinlove Shell Programming and Scripting 1 02-26-2012 03:22 AM
Show the diff in two files using awk gambit97 Shell Programming and Scripting 6 04-13-2011 11:56 PM
Compare two text files and Only show the differences CelvinSaran Shell Programming and Scripting 10 02-26-2010 04:56 AM
Two files one file is dependent and it does not show an output vinayrao Shell Programming and Scripting 2 01-19-2010 12:53 PM
Want to show files on web page deepa20 Shell Programming and Scripting 3 02-07-2006 09:53 AM



All times are GMT -4. The time now is 01:51 PM.