Need help to find difference between two files


 
Thread Tools Search this Thread
Operating Systems Solaris Need help to find difference between two files
# 1  
Old 05-26-2010
Error Need help to find difference between two files

I need to find the difference between two files in UNIX. I tried diff, but couldn't get it right.

There are two files:

file1:
Code:
       apple
       mango
       strawberry
       banana
       grape

file2:
Code:
       grape
       apple
       banana

I need an output file like below:
Code:
       mango
       strawberry

Can anyone please help? Thanks in advance...

Last edited by Yogesh Sawant; 05-26-2010 at 04:00 AM.. Reason: added code tags
# 2  
Old 05-26-2010
while read line;do
Code:
while read line;do
grep -qw "$line"  FILE2
[$? -ne 0 ] && { echo "$line" } 
done < FILE1

# 3  
Old 05-26-2010
You could also try:
Code:
$ grep -vf file2 file1
mango
strawberry

# 4  
Old 05-26-2010
Quote:
Originally Posted by rrstone
while read line;do
Code:
while read line;do
grep -qw "$line"  FILE2
[$? -ne 0 ] && { echo "$line" } 
done < FILE1


The following error occurs:
syntax error at line 7: `done' unexpected

---------- Post updated at 12:15 PM ---------- Previous update was at 12:14 PM ----------

Quote:
Originally Posted by scottn
You could also try:
Code:
$ grep -vf file2 file1
mango
strawberry

This error occurs:
grep: illegal option -- f
Usage: grep -hblcnsviw pattern file . . .
# 5  
Old 05-26-2010
I only said you could try Smilie

Code:
$ awk 'NR==FNR { F[$1]=1; next } !F[$1]' file2 file1
mango
strawberry

There's a space missing from rrstone's test is all:
Code:
[$? -ne 0 ]

should be
Code:
[ $? -ne 0 ]

edit: PS, if you're using Solaris, use /usr/xpg4/bin/awk or nawk

Last edited by Scott; 05-26-2010 at 03:24 AM..
# 6  
Old 05-26-2010
"The following error occurs:
syntax error at line 7: `done' unexpected"

I am sorry , there are 4 lines in my script .
# 7  
Old 05-26-2010
Quote:
Originally Posted by scottn
I only said you could try Smilie

Code:
$ awk 'NR==FNR { F[$1]=1; next } !F[$1]' file2 file1
mango
strawberry

There's a space missing from rrstone's test is all:
Code:
[$? -ne 0 ]

should be
Code:
[ $? -ne 0 ]


I guess today is my error day!!!
awk 'NR==FNR { F[$1]=1; next } !F[$1]' file2 file1
F[: Event not found.

---------- Post updated at 12:26 PM ---------- Previous update was at 12:24 PM ----------

Quote:
Originally Posted by rrstone
"The following error occurs:
syntax error at line 7: `done' unexpected"

I am sorry , there are 4 lines in my script .
Well, actually this was the script I wrote:
#!/bin/sh

while read line
do
grep -qw "$line" file2
[$? -ne 0 ] && { echo "$line" }
done < file1

---------- Post updated at 12:28 PM ---------- Previous update was at 12:26 PM ----------

Quote:
Originally Posted by scottn
I only said you could try Smilie

Code:
$ awk 'NR==FNR { F[$1]=1; next } !F[$1]' file2 file1
mango
strawberry

There's a space missing from rrstone's test is all:
Code:
[$? -ne 0 ]

should be
Code:
[ $? -ne 0 ]

edit: PS, if you're using Solaris, use /usr/xpg4/bin/awk or nawk

The same error occurs, even if the space problem is resolved
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find difference in content between two particular lines in two files

I have two files named Before.txt and After.txt: Now i want to find the difference in content between <Marker 1> and <Marker 2> in the two files. ---------- Post updated at 05:00 PM ---------- Previous update was at 04:50 PM ---------- Any help will be highly appreciated..:) (3 Replies)
Discussion started by: proactiveaditya
3 Replies

2. Homework & Coursework Questions

Script to find difference between 2 files by column

Hi , i am newbie to shell scripting and am trying to do the below job, A shell script to be run with a command like sh Compare.ksh file1.txt file2.txt 1 2 > file3.txt 1 2-are the key columns Consider the delimiter would be Tab or comma File 1: SK TEST NAME MATHS PHYSICS 21 1 AAA... (1 Reply)
Discussion started by: shakthi666
1 Replies

3. Shell Programming and Scripting

Script to find difference between 2 files by column

Hi , i am newbie to shell scripting and am trying to do the below job, A shell script to be run with a command like sh Compare.ksh file1.txt file2.txt 1 2 > file3.txt 1 2-are the key columns Consider the delimiter would be Tab or comma File 1: SK TEST NAME MATHS PHYSICS 21 1... (1 Reply)
Discussion started by: shakthi666
1 Replies

4. Shell Programming and Scripting

script to find whether the difference between two files in only additions but not modifications

i basically have 2 files and sdiff of the files is given below sdiff file1 file 2 control_file < path INDEX < size 613 < mode 0444 ... (1 Reply)
Discussion started by: under_cons
1 Replies

5. Shell Programming and Scripting

To find difference between two files on a whole

Hi, The requirement is to compare two files that has single column of records each. Comparison is to happen on a whole and not line by line. File1.txt 314589929 315611087 304924413 315989094 301171509 302984393 315609549 314593632 File2.txt 315611087 304924413 315989094 (2 Replies)
Discussion started by: anandek
2 Replies

6. UNIX for Advanced & Expert Users

Find difference between 2 files

I have 2 files as follows. file1.txt <cell>123</cell> <cell>345</cell> file2.txt <cell>123</cell> <cell>456</cell> out out should be output.txt <cell>456></cell> How do we achieve this> The difference betwenn the two files should be wirtten to the output file.. ... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

7. Shell Programming and Scripting

Find file size difference in two files using awk

Hi, Could anyone help me to solve this problem? I have two files "f1" and "f2" having 2 fields in each, a) file size and b) file name. The data are almost same in both the files except for few and new additional lines. Now, I have to find out and print the output as, the difference in the... (3 Replies)
Discussion started by: royalibrahim
3 Replies

8. UNIX for Dummies Questions & Answers

Find difference between two files

I have two files as below File1: a b c d File2: a b When i find the difference the output would be c&d.. How can i get my requirement...pls help... Many thanks in advance (10 Replies)
Discussion started by: jagadish_gaddam
10 Replies

9. UNIX for Dummies Questions & Answers

Find difference in two files

Hola, Tengo un texto texto1.txt con el siguiente contenido: Malaga Cadiz Sevilla Hola Y otro .txt texto2.txt con: Malaga Cadiz Sevilla Cordoba Huelva quiero obtener en otro .txt la diferencia entre estos dos archivos: (14 Replies)
Discussion started by: danietepa
14 Replies

10. Emergency UNIX and Linux Support

to find difference between two files

I have a file which gets appended with records daily..for eg. 1st day of the month i get 9 records ,2nd day 9 records .....till the last day in the month...the no of records may vary...i store the previous days file in a variable oldfile=PATH/previousdaysfile....i store the current days file in a... (6 Replies)
Discussion started by: ganesh_248
6 Replies
Login or Register to Ask a Question