Link to home
Start Free TrialLog in
Avatar of hocheeming
hocheeming

asked on

shell script to compare 2 files contents

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.
Avatar of ozo
ozo
Flag of United States of America image

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
Avatar of ghostdog74
ghostdog74

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

ASKER CERTIFIED SOLUTION
Avatar of Brian Utterback
Brian Utterback
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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