Operating System - HP-UX
1832366 Members
2701 Online
110041 Solutions
New Discussion

Re: compare entries in two files

 
rleon
Regular Advisor

compare entries in two files

Hi ..

I have two files and would like the output to only contain data that is not duplicated across both files.

filea contains:

a
b
c
i
j
k


fileb contains:
a
b
c
d
e
f
g
h
i
j
k

desired output
d
e
f
g
h
4 REPLIES 4
H.Merijn Brand (procura
Honored Contributor

Re: compare entries in two files

If both files are sorted

$ comm -3 file1 file2

-1: filter data only in file1
-2: filter data only in file2
-3: filter data in both files

If your first file contains a "filter" for file2

$ comm -13 file1 file2

or if you have GNU grep (but that is slower)

$ grep -F -x -f file1 file2

If file1 contains patters to filer out of file2

$ grep -v -f file1 file2

Note that in that case each line in file1 is expanded as being a regular expression

If unsorted and may contain dups, this lists all lines only in either file:

$ perl -wne'$e{$_}{$ARGV}++}END{print for sort grep{keys%{$e{$_}}==1}keys%e' file1 file2

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Alzhy
Honored Contributor

Re: compare entries in two files

diff filea fileb|egrep ">|<"|awk '{print $2}'

;^)
Hakuna Matata.
Alzhy
Honored Contributor

Re: compare entries in two files

very inefficient but works:

for x in `cat filea filebb|sort -u`;do
[ "$(cat a b|grep -w $x|wc -l)" -eq 1 ] && echo $x
done

;^)
Hakuna Matata.
muruganantham raju
Valued Contributor

Re: compare entries in two files

Hi,
Please run this command.

diff filea fileb | awk ' $1==">" || $1=="<" { print $2 }'