How to Get a Resumed (Counter) Output Using diff
To get a resumed (counter) output when using the diff
command in the command line, you can use the following options and techniques:
Basic ``diff`` Output
diff a b
Side-by-Side Output with Suppressed Common Lines
diff -y --suppress-common-lines a b
Brief Output (Shows Only Whether Files Differ)
diff --brief a b
Count the Number of Differences To count the number of differences, pipe the output of
diff
towc -l
:diff a b | wc -l
Summarized Output with Counts For a detailed summary, use a combination of
diff
andawk
to count additions, deletions, and changes:diff a b | awk '/^</ {a++} /^>/ {d++} END {print "Additions:", a, "Deletions:", d}'
Example
Suppose you have two files a
and b
:
File a:
line1
line2
line3
File b:
line1
line2
line4
Running:
diff a b
Output:
3c3
< line3
---
> line4
To count the number of differences:
diff a b | wc -l
Output:
2
To get a summarized output:
diff a b | awk '/^</ {a++} /^>/ {d++} END {print "Additions:", a, "Deletions:", d}'
Output:
Additions: 1 Deletions: 1
This will give you a count of the number of lines that are different between the two files.