Comparing files using a loop

Hi,
I'm trying to compare two files using a loop statement.. So far I've gotten the below but instead of outputting what line number is missing in File1 one, I'd like it to output the actual line that was found..

Can anyone give me some pointers..

#!/bin/ksh

counter=1
exec 3< File2.txt

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

How about this...

#!/bin/ksh

counter=1
exec 3< File2.txt

while read -u3 line
do
ln=$(grep "$line" File1.txt)
if [[ $? -eq 1 ]]
then
print "Line $counter ain't in the File1.txt file!!!" > Compare_result.txt
else
print $ln
fi
(( counter += 1 ))
done

Hi, tried that but i didn't get the outcome i was looking for...

Basically what I'm trying to do here is compare two folders on two separate servers.. One of the servers is our Production region (file1) and the other is a test region (file2).. I want to compare the rcode directories on both so that we can make sure they are both in sync.. I've tried using the diff command but it to doesn't work the way i want.. I'm aware that there's an rsync command out there but there seems to be some issue using that command with in our network..

I thought the best thing to do would be to ls -lrt the contents of both directories to txt files, move them to the same folder and use a loop command to compare them... I'm not great on scripting so trying to figure this out..

I've created both files and edited them using awk so that they just have the file size and file name contained in each txt file.

I want to check whats in file1 thats not in file2 and output it to another file.. Can anyone suggest how to do this??

And when you take the contents of the directory, make ls -1 (one not ele)

ls -1
awk 'NR==FNR{a[$0];next}$0 in a{next}1' file2 file1

Ls -1 would not give the file size, just the name.However I think the suggested nawk script would work anyway if file1 and file2 contained just the size and name on each line as suggested by the earlier post.Another way to do this is sort and then diff the files and grep which ever chevron > or < is appropriate from the output.

using perl:-
below code will give you the lines that are exists in file2 and not on file1

perl -e '@s{`cat file1.txt`}=( ); exists $s{$_} || print for `cat file2.txt` ;'

;);):wink:

I tried the awk command but I'm still getting results that are contained in both files. I had the same thing happen when i tried to use the diff command.. I did try and grep just for < so that it would just give me whats on file1 but i still had results come up that were contained in both files..

The reason i'm only outputting the size and filename is because we might have different permissions on each server for the files, and the owner and group names may also be different, so I don't want these flagging differences.. Each file contains a couple of thousand lines to compare..

I'm trying out using the comm command to see if that works..

have you made sure when you run the below awk command, you have put file2 before file1.

awk 'NR==FNR{a[$0];next}$0 in a{next}1' file2 file1

Ok, i changed around the format of the source files a bit.. They contained the file size first and then the name.. I switched them around.. Is this awk command just comparing the first colum or does it compare the whole sentence?

it compare the whole line or as you call sentence.

Ok, tried it the other way too and it seems to work.. Must of messed it up the first time around..

Thanks for your help man..

did you try my perl solution?

Using ksh , not sure if i can use perl to be honest..

did you test it? I think perl exist if awk exist in your system.

Is there a way of incorporating a send email email part if the awk commad finds a difference between the files? I just want to send a mail if there's an output and not send one if nothing is found..

awk 'NR==FNR{a[$0];next}$0 in a{next}1' file2 file1 > file3

I also found that the

comm -2 -3 file1 file2 > file3

seems to work to... Is there a way to send the email with that piece of code?

EDIT **
Would i be right in saying I could just use

if [[ -s file3 ]] ; then
mail -s "File not empty" email@email.com < file3
else
echo "File was empty" >> $LOG
fi; 

Do I need that extra ; after the fi?

No, you don't need that semi colon after fi.