Steam
Steam

Reputation: 9856

Fastest way of finding differences between two files in unix?

I want to find the difference between two files and then put only the differences in a third file. I saw different approaches using awk, diff and comm. Are there any more ?

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

eg.Copy differences between two files in unix

I need to know which is the fastest way of finding all the differences and listing them in a file for each of the cases below -

Case 1 - file2 = file1 + extra text appended.
Case 2 - file2 and file1 are different.

Upvotes: 21

Views: 123287

Answers (4)

James Bond 86
James Bond 86

Reputation: 1

This will work fast:

Case 1 - File2 = File1 + extra text appended.

grep -Fxvf File2.txt File1.txt >> File3.txt

File 1: 80 Lines File 2: 100 Lines File 3: 20 Lines

Upvotes: 0

pron
pron

Reputation: 161

Another option:

sort file1 file2 | uniq -u > file3

If you want to see just the duplicate entries use "uniq -d" option:

sort file1 file2 | uniq -d > file3

Upvotes: 16

P_M
P_M

Reputation: 328

You could also try to include md5-hash-sums or similar do determine whether there are any differences at all. Then, only compare files which have different hashes...

Upvotes: 1

danmc
danmc

Reputation: 1201

You could try..

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

or

grep -Fxvf file1 file2 > file3

or

diff file1 file2 | grep "<" | sed 's/^<//g'  > file3

or

join -v 2 <(sort file1) <(sort file2) > file3

Upvotes: 52

Related Questions