Get correct number of lines in diff output

diff()wc

I want to get the correct number of lines in the output of diff(specifically with -y and --suppress-common-lines options). Using a simple wc -l does not work, because if both files end without a newline and their last line is different wc -l won't count the last line.

Is there a simple and efficient solution to avoid this?

For example, if you have files "a":

a
b
c
d   #no newline here

And "b":

a
b
c
D    #no newline here

The output is:

$ diff -y --suppress-common-lines a b | wc -l
0

Which is obviously incorrect since diff does output a line.

Best Answer

There is no newline, so wc -l is correct. Instead, you want to count the number of start of lines. One way to do it:

$ diff -y --suppress-common-lines a b | grep '^' | wc -l
1
Related Question