Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way of finding differences between two files in unix?

Tags:

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. 
like image 481
Steam Avatar asked Aug 05 '13 23:08

Steam


People also ask

How do you find the difference in files in Unix?

Use the diff command to compare text files. It can compare single files or the contents of directories. When the diff command is run on regular files, and when it compares text files in different directories, the diff command tells which lines must be changed in the files so that they match.

How can I compare two files for differences?

Right-click on the first file. Click on “Select for Compare” from the menu. Proceed to right-click on the second file. Click on “Compare with Selected.

How to compare two files in Unix?

The file comparison command helps us to compare the files and find the similarities and differences between these files. The different file comparison commands used in Unix are cmp, comm, diff, dircmp, and uniq. Unix Video #8: Different ways of comparing two files in Unix. #1) cmp: This command is used to compare two files character by character.

How to compare binary files in Linux?

You can also use the diff command to compare binary files, but it will only tell you if the files are different unless you use the-s option.

How does diff work in Unix?

How does DIFF work in Unix? On Unix-like operating systems, the diff command analyzes two files and prints the lines that are different. In essence, it outputs a set of instructions for how to change one file to make it identical to the second file.

Which command is used to compare two files character by character?

#1) cmp: This command is used to compare two files character by character. Syntax: cmp [options] file1 file2 Example: Add write permission for user, group and others for file1. $ cmp file1 file2


2 Answers

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 
like image 169
danmc Avatar answered Oct 01 '22 15:10

danmc


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 
like image 45
pron Avatar answered Oct 01 '22 15:10

pron