Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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 momGiven a file (called junk2) with the following data:
9012
1234
3456
5678This script will give you the line in junk2 that is not in junk1:
#!/bin/ksh
counter=1
exec 3< junk1while 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 ))
doneWhen you run the script, your output will look like this:
Line 5 ain't in the junk2 file!!!

![]() |
![]() |
![]() |

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