[solved] Diff between two files by grep

My requiremeny is as follows,

I have two files

file a

A BONES RD,NHILL,3418,VIC 
37TH PARALLEL RD,DEEP LEAD,3385,VIC 
4 AK RD,OAKEY,4401,QLD 
A & J FARRS RD,BARMOYA,4703,QLD 
A B PATTERSON DR,ARUNDEL,4214,QLD 
A BLAIRS RD,BUCKRABANYULE,3525,VIC 

file b

A BONES RD,NHILL,3418,VIC 
37TH PARALLEL RD,DEEP LEAD,3385,VIC 
4 AK RD,OAKEY,4401,QLD 
A & J FARRS RD,BARMOYA,4703,QLD 
A B PATTERSON DR,ARUNDEL,4214,QLD 

On greping content of each row of file 1 in file 2, if no result found re-direct to file3
In this case file 3 should have below content

file 3

A BLAIRS RD,BUCKRABANYULE,3525,VIC

My code is as follows,

cat  file1| while read line
do
grep   -v  "$line'"  file2  > file3    
done

Output I' getting,

A BONES RD,NHILL,3418,VIC
37TH PARALLEL RD,DEEP LEAD,3385,VIC
4 AK RD,OAKEY,4401,QLD
A & J FARRS RD,BARMOYA,4703,QLD
A B PATTERSON DR,ARUNDEL,4214,QLD

Please help me,I tried grep -f file1 file2 > file3 but its getting following error

grep: illegal option -- f
Usage: grep -hblcnsviw pattern file . . .

please give u r suggestion with out using -f option

rm -f file3
while read line
do
  grep "$line'" file2 || echo "$line" >> file3
done < file1


A BLAIRS RD,BUCKRABANYULE,3525,VIC

for me the code not working,I'm using sun solaris 5.8.My o/p as follows

output

A BONES RD,NHILL,3418,VIC
37TH PARALLEL RD,DEEP LEAD,3385,VIC
4 AK RD,OAKEY,4401,QLD
A & J FARRS RD,BARMOYA,4703,QLD
A B PATTERSON DR,ARUNDEL,4214,QLD

Won't be easy using diff?
Anyway,

cat file1.txt | while read line; do
        if [ -z "`grep "$line" file2.txt`" ]; then
                echo $line >> file3.txt
        fi
done

Albert.

Simple to work

sdiff file1 file2 | grep "<" | cut -d"<" -f1 | tr -d " " >file 3

Hi Albert/All,

Could you please explain the -z option in if condition.

Thanks,
Nagesh

Hi.

There was a slight cut and paste error in my previous post.

Here's a modified one (tested on Solaris 8)

rm -f file3
while read line
do
  grep "$line" file2 > /dev/null || echo "$line" >> file3
done < file1
-z string      True  if  the  length  of  string string is
               zero.

test(1)

I suggest to get a shell reference, if you need to program scripts often. In bash: GNU bash manual - GNU Project - Free Software Foundation (FSF), available on-line and in several formats to be downloaded.
As scottn said, [-z] option checks whether length of string is 0. You can find out many other conditional expressions in: Bash Conditional Expressions

Albert.

Hi Albert/All,

I have few queries regarding the code as i was a shell scirpt begginer,

(1) In while loop we are checking whether the line exist or not then why we again checking in if loop
(2) I tried the code by removing z option then the output totaaly changing,as we are using -z to find length why the output changes

output with out using -z

A BONES RD,NHILL,3418,VIC
37TH PARALLEL RD,DEEP LEAD,3385,VIC
4 AK RD,OAKEY,4401,QLD
A & J FARRS RD,BARMOYA,4703,QLD
A B PATTERSON DR,ARUNDEL,4214,QLD

Hi.

(1) it's not necessary
(2) by removing -z you've negated the result (i.e. to get all the lines in file2 that are in file1)

You can also do this in awk (nawk or /usr/xpg4/bin/awk on Solaris):

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


A BLAIRS RD,BUCKRABANYULE,3525,VIC

Hi,

In script I posted I checked whether a line is in the other file. To do that I just check its length. If length is 0 it means grep has no output. So, that line is not in file2, and it can be printed to file3. In two steps:

exists=�grep "$line" file2.txt`   ##finds "$line" in file2
if [ -z $exists ]; then             ## Test whether $exists has length 0
   echo $line >> file3.txt         ## If so, line is not in file2. Then I print to file3
fi

Maybe for you it's easier to understand scottn script, or ramkrix simpler way using sdiff.

Albert.

Hi All,

The script working fine now I understood clearly the codes including albert code

Thanks a lot..

I can use :

grep -v -f file1 file2 -x