[solved] Diff between two files by grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [solved] Diff between two files by grep
# 1  
Old 08-02-2010
[solved] Diff between two files by grep

My requiremeny is as follows,

I have two files

file a
Code:
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
Code:
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
Code:
A BLAIRS RD,BUCKRABANYULE,3525,VIC

My code is as follows,
Code:
cat  file1| while read line
do
grep   -v  "$line'"  file2  > file3    
done

Output I' getting,
Code:
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
Code:
grep: illegal option -- f
Usage: grep -hblcnsviw pattern file . . .

please give u r suggestion with out using -f option

Moderator's Comments:
Mod Comment Use code tags please, ty.
feelmyfrd
# 2  
Old 08-02-2010
Code:
rm -f file3
while read line
do
  grep "$line'" file2 || echo "$line" >> file3
done < file1


A BLAIRS RD,BUCKRABANYULE,3525,VIC

# 3  
Old 08-02-2010
Diff between two files by grep

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

output
Code:
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

feelmyfrd
# 4  
Old 08-02-2010
Won't be easy using diff?
Anyway,
Code:
cat file1.txt | while read line; do
        if [ -z "`grep "$line" file2.txt`" ]; then
                echo $line >> file3.txt
        fi
done

Albert.
# 5  
Old 08-02-2010
Simple to work
Code:
sdiff file1 file2 | grep "<" | cut -d"<" -f1 | tr -d " " >file 3

# 6  
Old 08-02-2010
Hi Albert/All,

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

Thanks,
Nagesh
feelmyfrd
# 7  
Old 08-02-2010
Hi.

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

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

Code:
rm -f file3
while read line
do
  grep "$line" file2 > /dev/null || echo "$line" >> file3
done < file1

Code:
-z string      True  if  the  length  of  string string is
               zero.

test(1)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

2. Shell Programming and Scripting

Grep -v -f and sort|diff which way is faster

Hi Gurus, I have two big files. I need to compare the different. currently, I am using sort file1 > file1_temp; sort file2 > file2_tmp diff file1_tmp file2_tmp I can use command grep -v -f file1 file2 just wondering which way is fast to compare two big files. Thanks... (4 Replies)
Discussion started by: ken6503
4 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Grep multiple files and display first match

I have a need to grep a large number of files, but only display the first result from each file. I have tried to use grep, but am not limited to it. I can use perl and awk as well. Please help! (9 Replies)
Discussion started by: dbiggied
9 Replies

4. Shell Programming and Scripting

[Solved] Issue with grep

Hi everyone I am trying to write a script to check if file systems are mounted, and also validate the permission; then do a whole bunch of other things. I am facing a problem with grep. For example, if the mountpoints are: /dev/XYZ_lv /abc/XYZ jfs2 Nov 25 20:36... (4 Replies)
Discussion started by: nimo
4 Replies

5. Shell Programming and Scripting

[Solved] Help with grep script

Hi, I'm having trouble with a script to copy one line out of multiple files in a directory and copy to a file called test. I've tried the code below but it copies one line out of the first file multiple times not one line out of all the files. Would someone help? I'm very new to all this. ... (8 Replies)
Discussion started by: bob101
8 Replies

6. Shell Programming and Scripting

Diff between grep .* file name and grep '.*' filename

Hi, Can anyone let me know what is difference between grep .* foo.c grep '.*' foo.c I am not able to understand what is exact difference. Thanks in advance (2 Replies)
Discussion started by: SasDutta
2 Replies

7. Shell Programming and Scripting

[solved] merging two files and writing to another file- solved

i have two files as file1: 1 2 3 file2: a b c and the output should be: file3: 1~a 2~b 3~c (1 Reply)
Discussion started by: mlpathir
1 Replies

8. Shell Programming and Scripting

Solved: finding diff in files

i want to compare 2 files and generate below 3 files: 1. new lines 2. updated lines 3. deleted lines are there any one liners for each one of them. Note the method to find duplicates is based on field 1, values are separated by '|' example: test1 (older file) 1|XXX 2|YYY... (3 Replies)
Discussion started by: manishma71
3 Replies

9. Shell Programming and Scripting

Find duplicates from multuple files with 2 diff types of files

I need to compare 2 diff type of files and find out the duplicate after comparing each types of files: Type 1 file name is like: file1.abc (the extension abc could any 3 characters but I can narrow it down or hardcode for 10/15 combinations). The other file is file1.bcd01abc (the extension... (2 Replies)
Discussion started by: ricky007
2 Replies

10. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies
Login or Register to Ask a Question