How can I get diff to show only added and deleted lines? If diff can’t do it, what tool can?

Try comm

Another way to look at it:

  • Show lines that only exist in file a: (i.e. what was deleted from a)

      comm -23 a b
    
  • Show lines that only exist in file b: (i.e. what was added to b)

      comm -13 a b
    
  • Show lines that only exist in one file or the other: (but not both)

      comm -3 a b | sed 's/^\t//'
    

(Warning: If file a has lines that start with TAB, it (the first TAB) will be removed from the output.)

Sorted files only

NOTE: Both files need to be sorted for comm to work properly. If they aren’t already sorted, you should sort them:

sort <a >a.sorted
sort <b >b.sorted
comm -12 a.sorted b.sorted

If the files are extremely long, this may be quite a burden as it requires an extra copy and therefore twice as much disk space.

Leave a Comment