Solved

shell script to compare 2 files contents

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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 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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now