Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

diff command to get number of different lines only

Can I use the diff command to find out how many lines do two files differ in?

I don't want the contextual difference, just the total number of lines that are different between two files. Best if the result is just a single integer.

like image 270
gmmo Avatar asked Dec 01 '14 20:12

gmmo


People also ask

What is the command to count only the number of lines in the file?

Use the wc command to count the number of lines, words, and bytes in the files specified by the File parameter. If a file is not specified for the File parameter, standard input is used.

Which command is used for difference?

diff stands for difference. This command is used to display the differences in the files by comparing the files line by line.


2 Answers

diff can do all the first part of the job but no counting; wc -l does the rest:

diff -y --suppress-common-lines file1 file2 | wc -l

like image 70
digitmaster Avatar answered Sep 22 '22 23:09

digitmaster


Yes you can, and in true Linux fashion you can use a number of commands piped together to perform the task.

First you need to use the diff command, to get the differences in the files.

diff file1 file2 

This will give you an output of a list of changes. The ones your interested in are the lines prefixed with a '>' symbol

You use the grep tool to filter these out as follows

diff file1 file2 | grep "^>" 

finally, once you have a list of the changes your interested in, you simply use the wc command in line mode to count the number of changes.

diff file1 file2 | grep "^>" | wc -l 

and you have a perfect example of the philosophy that Linux is all about.

like image 26
Zhilong Jia Avatar answered Sep 19 '22 23:09

Zhilong Jia