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:
apple
mango
strawberry
banana
grape
file2:
grape
apple
banana
I need an output file like below:
mango
strawberry
Can anyone please help? Thanks in advance...
while read line;do
while read line;do
grep -qw "$line" FILE2
[$? -ne 0 ] && { echo "$line" }
done < FILE1
Scott
3
You could also try:
$ grep -vf file2 file1
mango
strawberry
The following error occurs:
syntax error at line 7: `done' unexpected
---------- Post updated at 12:15 PM ---------- Previous update was at 12:14 PM ----------
This error occurs:
grep: illegal option -- f
Usage: grep -hblcnsviw pattern file . . .
Scott
5
I only said you could try
$ awk 'NR==FNR { F[$1]=1; next } !F[$1]' file2 file1
mango
strawberry
There's a space missing from rrstone's test is all:
[$? -ne 0 ]
should be
[ $? -ne 0 ]
edit: PS, if you're using Solaris, use /usr/xpg4/bin/awk or nawk
"The following error occurs:
syntax error at line 7: `done' unexpected"
I am sorry , there are 4 lines in my script .
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 ----------
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 ----------
The same error occurs, even if the space problem is resolved
#!/bin/sh
while read line; do
grep -qw "$line" file2
[ $? -ne 0 ] && {
echo "$line"
}
done < file1
This should work:
diff <(sort file1) <(sort file2) | sed 's/^[^ ]*//'
you can try :-
sort -o file1 file1
sort -o file2 file2
comm -23 file1 file2
sort file1 file2 | uniq -u
hergp
12
On Solaris you need the grep in /usr/xpg4/bin in order to use the -f switch:
$ /usr/xpg4/bin/grep -vf file2 file1
mango
strawberry