148

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

Yarin
  • 173,523
  • 149
  • 402
  • 512
Balualways
  • 4,250
  • 10
  • 38
  • 51
  • I tried diff but it generates some numbers and other symbols in front of different lines that makes it difficult for me to compare files. – Sun Jul 13 '15 at 15:57

14 Answers14

263

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

sorpigal
  • 25,504
  • 8
  • 57
  • 75
  • 2
    Remember that two files must be sorted and is unique – andy Oct 16 '15 at 12:54
  • 10
    You can group the options together: `comm -23` – Paolo M Oct 22 '15 at 12:06
  • What does "sorted" mean? That the lines have the same order? Then it's probably fine for most use cases - as in, checking for what lines have been added by comparing with a backed-up older version. If newly added lines cannot be between the existing lines, that's more of an issue. – Egor Hans Nov 12 '17 at 20:00
  • @EgorHans: if the file has e.g. lines containing integers such as "3\n1\n3\n2\n" lines must first be reordered in to ascending or descending order e.g. "\1\n2\n3\n3\n" with duplicates adjacent. That is "sorted" and both files must be sorted in a similar manner. When the newer file has new lines it does not matter if they are "between existing lines" because after the sort they are not, they're in sorted order. – sorpigal Nov 27 '17 at 20:06
50

The Unix utility diff is meant for exactly this purpose.

$ diff -u file1 file2 > file3

See the manual and the Internet for options, different output formats, etc.

Thanatos
  • 42,585
  • 14
  • 91
  • 146
  • 11
    That does not do the job requested; it inserts a bunch of extra characters, even with the use of commandline switches suggested in other answers. – xenocyon Jan 26 '16 at 22:25
25

Consider this:
file a.txt:

abcd
efgh

file b.txt:

abcd

You can find the difference with:

diff -a --suppress-common-lines -y a.txt b.txt

The output will be:

efgh 

You can redirict the output in an output file (c.txt) using:

diff -a --suppress-common-lines -y a.txt b.txt > c.txt

This will answer your question:

"...which contains the lines in file1 which are not present in file2."

Neilvert Noval
  • 1,655
  • 2
  • 15
  • 21
  • 2
    There are two limitations to this answer: (1) it only works for short lines (less than 80 chars by default, although this can be modified) and, more important, (2) it add a "<" at the end of each line that must be taken away with another program (e.g. awk, sed). – sergut Dec 30 '15 at 10:29
  • In many cases, you'll also want to use `-d`, which will make `diff`do its best to find the smallest possible diff. `-i`, `-E`, `-w`, `-B` and `--suppress-blank-empty` can also be useful occasionally, although not always. If you don't know what fits your use case, try `diff --help` first (which is generally a good idea when you don't know what a command can do). – Egor Hans Nov 12 '17 at 20:24
  • Also, using --line-format=%L, you keep diff from generating any extra characters (at least, the help says it works like this, yet about to try it out). – Egor Hans Nov 14 '17 at 16:10
  • Also this is shorter and seems works the same https://stackoverflow.com/a/27667185/1179925 – mrgloom Jun 03 '19 at 16:56
17

Yet, no grep solution?

  • lines which are exist only in file2:

      grep -Fxvf file1 file2 > file3
    
  • lines which are exist only in file1:

      grep -Fxvf file2 file1 > file3
    
  • lines which are exist in both files:

      grep -Fxf file1 file2 > file3
    

Switches description (see also man grep):

  • The -F tells grep to interpret PATTERNS as fixed strings, not regular expressions.
  • The -x tells grep to select only those matches that exactly match the whole line not partiall match.
  • With the -f, grep obtains the patterns from FILE, one per line.
  • The -v just inverts the sense of matching, to select non-matching lines.
αғsнιη
  • 2,627
  • 2
  • 25
  • 38
13

Sometimes diff is the utility you need, but sometimes join is more appropriate. The files need to be pre-sorted or, if you are using a shell which supports process substitution such as bash, ksh or zsh, you can do the sort on the fly.

join -v 1 <(sort file1) <(sort file2)
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • 1
    You should get a medal for this! It was exactly what I was looking for the last 2 hours – Zatarra Nov 04 '19 at 23:03
  • Join is really so useful and fast. It can be use for many cases like finding difference just like this one, or finding commons in two files. – cem çetin Sep 20 '21 at 13:41
6

Try

sdiff file1 file2

It ususally works much better in most cases for me. You may want to sort files prior, if order of lines is not important (e.g. some text config files).

For example,

sdiff -w 185 file1.cfg file2.cfg
Tagar
  • 13,911
  • 6
  • 95
  • 110
  • 1
    Nice utility! I love how it marks the differentiating lines. Makes it much easier to compare configs. This together with sort is a deadly combo (e.g. `sdiff <(sort file1) <(sort file2)`) – jmagnusson Jan 19 '18 at 17:42
4

If you need to solve this with coreutils the accepted answer is good:

comm -23 <(sort file1) <(sort file2) > file3

You can also use sd (stream diff), which doesn't require sorting nor process substitution and supports infinite streams, like so:

cat file1 | sd 'cat file2' > file3

Probably not that much of a benefit on this example, but still consider it; in some cases you won't be able to use comm nor grep -F nor diff.

Here's a blogpost I wrote about diffing streams on the terminal, which introduces sd.

mlg
  • 1,447
  • 15
  • 19
4

You could use diff with following output formatting:

diff --old-line-format='' --unchanged-line-format='' file1 file2

--old-line-format='' , disable output for file1 if line was differ compare in file2.
--unchanged-line-format='', disable output if lines were same.

αғsнιη
  • 2,627
  • 2
  • 25
  • 38
4

I'm surprised nobody mentioned diff -y to produce a side-by-side output, for example:

diff -y file1 file2 > file3

And in file3(different lines have a symbol | in middle):

same     same
diff_1 | diff_2
xtluo
  • 1,961
  • 18
  • 26
2

Many answers already, but none of them perfect IMHO. Thanatos' answer leaves some extra characters per line and Sorpigal's answer requires the files to be sorted or pre-sorted, which may not be adequate in all circumstances.

I think the best way of getting the lines that are different and nothing else (no extra chars, no re-ordering) is a combination of diff, grep, and awk (or similar).

If the lines do not contain any "<", a short one-liner can be:

diff urls.txt* | grep "<" | sed 's/< //g'

but that will remove every instance of "< " (less than, space) from the lines, which is not always OK (e.g. source code). The safest option is to use awk:

diff urls.txt* | grep "<" | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}'

This one-liner diffs both files, then filters out the ed-style output of diff, then removes the trailing "<" that diff adds. This works even if the lines contains some "<" themselves.

sergut
  • 289
  • 2
  • 8
  • 1
    comm doesn't require sorting (in newer versions?) - just use --nocheck-order. I use this a lot when manipulating csvs from the CLI – ak5 Oct 06 '16 at 15:57
2
diff a1.txt a2.txt | grep '> ' | sed 's/> //' > a3.txt

I tried almost all the answers in this thread, but none was complete. After few trails above one worked for me. diff will give you difference but with some unwanted special charas. where you actual difference lines starts with '> '. so next step is to grep lines starts with '> 'and followed by removing the same with sed.

tollin jose
  • 845
  • 4
  • 12
  • 23
  • 1
    This is a bad idea. You would also need to modify lines starting with `<`. You will see this if you swap the order of the input files. Even if you did this you would want to omit `grep` by using more sed: ` diff a1 a2 | sed '/> /s///'` This can still break lines containing `>` or `<` in the right situation and **still** leaves extra lines describing line numbers. If you wanted to try this approach a better way would be: `diff -C0 a1 a2 | sed -ne '/^[+-] /s/^..//p'`. – sorpigal Aug 06 '17 at 21:03
1

Use the Diff utility and extract only the lines starting with < in the output

Capslockk
  • 68
  • 1
  • 1
  • 5
0

If you have a CSV file with single or even multiple columns, you can do these line by line "diff" operations using the sqlite3 embedded db. It comes with python, so should be available on most linux/macs. You can script the sqlite3 commands on the bash shell without needing to write python.

  1. Create your a.csv and b.csv files
  2. Ensure sqlite3 is installed using the command "sqlite3 -help"
  3. Run the below commands directly on the Linux/Mac shell (or put it in a script)
echo "
.mode csv
.import a.csv atable
.import b.csv btable
create table result as select * from atable EXCEPT select * from btable;
.output result.csv
select * from result ;
.quit
" | sqlite3 temp.db

Note : Ensure there is a newline for each of the sqlite3 commands.

How it works

  1. Import the 2 csvs into "atable" and "btable" respectively.
  2. Use the "except" sql operator to select the data available in "atable" but missing in "btable". Create a "result" table using the select query statement
  3. Output the result table to result.csv by running "select * from result;"

If you need to operate on specific columns, sqlite3 or any db is the way to go.

I have tried diff'ing on multiple GB files using the builtin diff and comm tools. Sqlite beats linux utilities by a mile.

Thyag
  • 1,217
  • 13
  • 14
0
linecount=0
while IFS= read -r line1; do
  let linecount=linecount+1
  IFS= read -r line2  < $2

  if [ "$line1" != "$line2" ] ; then
    echo "============diff: $linecount"
    echo "LINE1 $line1";
    echo "LINE2 $line2";
    echo ""
  fi
done < $1