[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
# 8  
Old 08-02-2010
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.
# 9  
Old 08-02-2010
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
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
# 10  
Old 08-02-2010
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):

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


A BLAIRS RD,BUCKRABANYULE,3525,VIC

# 11  
Old 08-02-2010
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:
Code:
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.
# 12  
Old 08-02-2010
Hi All,

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

Thanks a lot..
feelmyfrd
# 13  
Old 08-03-2010
I can use :
Code:
grep -v -f file1 file2 -x

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