command-linediff

How can I show lines in common (reverse diff)?


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.

File 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)

File bar:

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

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)

Solution

  • On *nix, you can use comm. The answer to the question is:

    comm -1 -2 file1.sorted file2.sorted 
    # where file1 and file2 are sorted and piped into *.sorted
    

    Here's the full usage of 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.