Compare two files line by line and generate the difference in another file

Balualways picture Balualways · Dec 28, 2010 · Viewed 371.8k times · Source

I want to compare file1 with file2 and generate a file3 which contains the lines in file1 which are not present in file2.

Answer

sorpigal picture sorpigal · Dec 28, 2010

diff(1) is not the answer, but comm(1) is.

NAME
       comm - compare two sorted files line by line

SYNOPSIS
       comm [OPTION]... FILE1 FILE2

...

       -1     suppress lines unique to FILE1

       -2     suppress lines unique to FILE2

       -3     suppress lines that appear in both files

So

comm -2 -3 file1 file2 > file3

The input files must be sorted. If they are not, sort them first. This can be done with a temporary file, or...

comm -2 -3 <(sort file1) <(sort file2) > file3

provided that your shell supports process substitution (bash does).