views:

1502

answers:

2

I have a series of text files for which I'd like to know the lines in common rather than the lines which are different between them. Command line unix or windows is fine.

foo:

linux-vdso.so.1 =>  (0x00007fffccffe000)
libvlc.so.2 => /usr/lib/libvlc.so.2 (0x00007f0dc4b0b000)
libvlccore.so.0 => /usr/lib/libvlccore.so.0 (0x00007f0dc483f000)
libc.so.6 => /lib/libc.so.6 (0x00007f0dc44cd000)

bar:

libkdeui.so.5 => /usr/lib/libkdeui.so.5 (0x00007f716ae22000)
libkio.so.5 => /usr/lib/libkio.so.5 (0x00007f716a96d000)
linux-vdso.so.1 =>  (0x00007fff737ff000)

So, given these two files above the output of the desired utility would be akin to file1:line_number, file2:line_number == matching text (just a suggestion, I really don't care what the syntax is):

foo:1, bar:3 == linux-vdso.so.1 =>  (0x00007fffccffe000)

thanks.

+7  A: 

On *nix, you can use comm.

comm [-1] [-2] [-3 ] file1 file2
-1 Suppress the output column of lines unique to file1.
-2 Suppress the output column of lines unique to file2.
-3 Suppress the output column of lines duplicated in file1 and file2.

Also note that it is important to sort the files before using comm, as mentioned in the man pages.

Daniel Lew
comm [-1] [-2] [-3 ] file1 file2-1 Suppress the output column of lines unique to file1. -2 Suppress the output column of lines unique to file2. -3 Suppress the output column of lines duplicated in file1 and file2.
ojblass
@ojblass: Added this to the answer.
Matt J
I discovered it is important the files be sorted before using comm. Perhaps add that to the answer.
matt wilkie
+4  A: 

Was asked here before: http://stackoverflow.com/questions/373810/unix-command-to-find-lines-common-in-two-files

You could also try with perl (credit goes here)

perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/'  file1 file2
ChristopheD
thanks. I would have like to accepted both answers, as the perl one liner is cross platform. Comm gets the nod because it is simpler.
matt wilkie