Solved

shell script to compare 2 files contents

Posted on 2007-11-25
4
5,783 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 84

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:
blu earned 125 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 84

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

837 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