Operating System - HP-UX
1820619 Members
1860 Online
109626 Solutions
New Discussion юеВ

Re: Command opposite to diff

 
SOLVED
Go to solution
Sailesh_1
Advisor

Command opposite to diff

Hey folks,

I'm looking for a unix (hpux 11.00) command that does just reverse of diff command. instead of showing lines that are different, I want it to show lines that are the same.

can someone tell me please!!

Thanks,
Sailesh
14 REPLIES 14
Geoff Wild
Honored Contributor
Solution

Re: Command opposite to diff

Yes - cmp

man cmp

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Robert Kerr
Advisor

Re: Command opposite to diff

Depending on your specific task... try the manpage on cmp and comm?
Sridhar Bhaskarla
Honored Contributor

Re: Command opposite to diff

Hi Sailesh,

'comm' is the one you are looking for. Look at the man page of comm. You are interested in -3 option which is "Lines that appear in both files".

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
john korterman
Honored Contributor

Re: Command opposite to diff

Hi,
perhaps the comm command could be useful...

regards,
John K.
it would be nice if you always got a second chance
Sailesh_1
Advisor

Re: Command opposite to diff

Thanks to you all who responded..That's what I'm looking for.

Sailesh
Jeff Schussele
Honored Contributor

Re: Command opposite to diff

Hi Sailesh,

Although I always use comm, it should be noted that
sdiff -l file1 file2
will show only identical lines on the left side of output.
and
sdiff file1 file2
will outpout lines w/o a "|", "<" or ">" that are identical.
and finally
sdiff -o file.out file1 file2
will print identical lines unaltered but will prompt for action on diffs.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Pete Randall
Outstanding Contributor

Re: Command opposite to diff

I always though it should be "comm -3" but I learned otherwise in a thread last week:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=388167

It's actually "comm -12" - try it!


Pete

Pete
H.Merijn Brand (procura
Honored Contributor

Re: Command opposite to diff

Shridar, it's reverse

-1 don't show lines only in file 1
-2 don't show lines only in file 2
-3 don't show lines in both files

comm is the command, but the option is not -3, but -12

And please also read thread http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=388167 where the same question is answwred in more depth, and I also post what differs between comm, and the perl and sed solution that also work on unsorted files:

perl -e'$f=pop;%l=map{$_=>1}<>;@ARGV=($f);for(<>){exists$l{$_}and print}' fileA fileB

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sridhar Bhaskarla
Honored Contributor

Re: Command opposite to diff

Hi Sailesh,

Pete is correct. comm -12 will display the common lines provided the files are sorted. So, you will need to create two temporary files and then use comm against them.

Comm with -3 suppresses column 3. So, you would use

comm file1 file2 |awk '{print $3}'

to get the result which is as same as comm -12

Key is that the files are to be sorted.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: Command opposite to diff

Gotcha Merijn - How deceiving a man page can be?

In fact it is a mere context miss

Column 1: Lines that appear only in file1,
Column 2: Lines that appear only in file2,
Column 3: Lines that appear in both files.

Where the context is "comm produces a three column output".

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
H.Merijn Brand (procura
Honored Contributor

Re: Command opposite to diff

but the nag is that the columns do NOT evenly distribute. Your awk solution is very void:

Try below yourself, because the forum strips whitespace!

lt09:/home/merijn 101 > cat >x0
aa
bb
dd
ff
gg
hh
lt09:/home/merijn 102 > cat >x1
aa
cc
ff
hh
lt09:/home/merijn 103 > comm x0 x1
aa
bb
cc
dd
ff
gg
hh
lt09:/home/merijn 104 >


And now with duplicate lines (still sorted!)

lt09:/home/merijn 104 > cat > x0
aa
bb
dd
ff
ff
gg
hh
lt09:/home/merijn 105 > cat >x1
aa
cc
ff
hh
hh
lt09:/home/merijn 106 > comm x0 x1
aa
bb
cc
dd
ff
ff
gg
hh
hh
lt09:/home/merijn 107 > perl -e'$f=pop;%l=map{$_=>1}<>;@ARGV=($f);for(<>){exists$l{$_}and print}' x0 x1
aa
ff
hh
hh
lt09:/home/merijn 108 > lt09:/home/merijn 108 > perl -e'$f=pop;%l=map{$_=>1}<>;@ARGV=($f);for(<>){exists$l{$_}and print}' x1 x0
aa
ff
ff
hh
lt09:/home/merijn 109 >

Hope this sheds some light

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sridhar Bhaskarla
Honored Contributor

Re: Command opposite to diff

Well - I wish HP could replace it's comm command with your perl script :-)

grep -f file1 file2

If we want a simple solution. No sorting is needed.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
H.Merijn Brand (procura
Honored Contributor

Re: Command opposite to diff

No won't work. See other thread

what if file1 has wildcards?

# cat >file1
.*
# cat >file2
aa
bb
# grep -f file1 file2
aa
bb
# grep -f file2 file1
#

as also from the other thread one uses GNU grep, it might work:

# grep -xFf file1 file2

but not all of us are so lucky to have taken the time to have HP's grep replaced with GNU grep

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sridhar Bhaskarla
Honored Contributor

Re: Command opposite to diff

That's a great show Merijn. Thanks. Ofcourse Enjoyed and had fun.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try