Computing.Net > Forums > Unix > Comparing 2 files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Comparing 2 files

Reply to Message Icon

Name: sureshht
Date: February 6, 2007 at 05:42:15 Pacific
OS: AIX 5L
CPU/Ram: 4 CPU/8GB RAM
Product: IBM
Comment:

Hi,
I have to compare 2 files using shell script. I need to take the each entry/line from the first file and compare it with 2nd file whether that entry exists in second file or not. If found...ok..else I have to send warning msg.
Please help me.

Thanks and regards
Suresh


sureshht



Sponsored Link
Ads by Google

Response Number 1
Name: thepubba1
Date: February 6, 2007 at 08:08:05 Pacific
Reply:

I'm assuming that their is a possibility that the entries are not in the same order in both files, correct? If they are in the same order, I'd suggest you check the man pages for the diff command. Otherwise, you can loop through one file, reading each line and use grep to see if it is in the other file.

Given a file (called junk1) with the following data:

1234
5678
9012
3456
Hi mom

Given a file (called junk2) with the following data:

9012
1234
3456
5678

This script will give you the line in junk2 that is not in junk1:

#!/bin/ksh

counter=1
exec 3< junk1

while read -u3 line
do
grep "$line" junk2 1>/dev/null
if [[ $? -eq 1 ]]
then
print "Line $counter ain't in the junk2 file!!!"
fi
(( counter += 1 ))
done

When you run the script, your output will look like this:

Line 5 ain't in the junk2 file!!!


0

Response Number 2
Name: sureshht
Date: February 6, 2007 at 10:07:44 Pacific
Reply:

Hi,

Script is exactly what i wanted.
Thanks a lot for your help

Suresh

sureshht


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Comparing 2 files

Compare 2 files inode modification www.computing.net/answers/unix/compare-2-files-inode-modification/5866.html

Compare 2 files if it's same r not www.computing.net/answers/unix/compare-2-files-if-its-same-r-not/8196.html

Comparing 2 files columnwise www.computing.net/answers/unix/comparing-2-files-columnwise/8340.html