Solved

shell script to compare 2 files contents

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

803 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