Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

shell script to compare 2 files contents

Posted on 2007-11-25
4
Medium Priority
?
5,823 Views
Last Modified: 2013-12-27
Hi,
I would like to create a shell script to compare 2 files.
content of file1:
1,2,abcd,4
2,2,abcd,4
3,4,bcde,5
5,5,xcvb,6
content of file2:
0,2,abcd,4
1,2,abcd,4
2,2,abcd,4
3,4,bcde,5
for those lines in file 1 and not in file2 output to a text file.
for those lines in file2 and not in file1 output to another text file.
So, after running the script,
the 1st text file should give me: 5,5,xcvb,6
the 2nd text file should give me:0,2,abcd,4
Thanks alots.
0
Comment
Question by:hocheeming
  • 2
4 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 20348543
perl -F, -ane '$h{$F[0]}{$n}=$_;$n+=eof;END{ @f=map{open local $f,">$_" and $f or die $_}qw(1st.file 2nd.file); print {$f[(keys %$_)[0]]} values %$_ for grep keys %$_ == 1,values %h}' file1 file2
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20348739
while i am impressed with the one-liner solution, I don't think a beginner would understand it. A solution, IMO, should be made understandable and readable, esp to OPs who are beginners.

@OP, since you are most likely working in Solaris, as seen in the zone where you asked this question, here's a nawk suggestion. Tested on Solaris 8 using only your sample files.


#!/bin/sh
nawk 'FNR==NR{a[$0];next}
{
 if ( !($0 in a) )
 {
    print  > "second-file.txt"
 }
 b[$0]
}
END {
    for ( i in a) {
      if ( !(i in b ) )  {
         print i > "first-file.txt"
      }
    }
}
' file1 file2

Open in new window

0
 
LVL 22

Accepted Solution

by:
Brian Utterback earned 250 total points
ID: 20349525
If the files are sorted (as they appear to be) then you can do it very simply like this:

comm -1 -3 file1 file2 > second-file.txt
comm -2 -3 file1 file2 > first-file.txt
0
 
LVL 85

Expert Comment

by:ozo
ID: 20355967
Did the example contents change while I was composing an answer?
I could have sworn the '2,abcd,4' part was different for the two files, which is why I went to the trouble to compare based on just the first comma separated field in order to produce the desired output instead of using comm, which would have also found the places where the '2,abcd,4' part differed
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question