Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count differences between two files on linux?

Tags:

shell

diff

count

I need to work with large files and must find differences between two. And I don't need the different bits, but the number of differences.

To find the number of different rows I come up with

diff --suppress-common-lines --speed-large-files -y File1 File2 | wc -l 

And it works, but is there a better way to do it?

And how to count the exact number of differences (with standard tools like bash, diff, awk, sed some old version of perl)?

like image 463
Zsolt Botykai Avatar asked Oct 14 '09 14:10

Zsolt Botykai


People also ask

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.

What command is used to compare the files in Linux?

cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not.


1 Answers

If you want to count the number of lines that are different use this:

diff -U 0 file1 file2 | grep ^@ | wc -l 

Doesn't John's answer double count the different lines?

like image 128
Josh Avatar answered Oct 22 '22 06:10

Josh