Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding how many lines are different between two files

Tags:

shell

unix

pipe

wc

Write another script that uses a command pipeline to take 2 files as parameters, compare their contents and count how many lines are different. You will use wc –l to count the differing lines.

I have tried everything I can think of to do this. I have tried cmp, comm, and diff. I am not looking for a complete solution, just a push in the right direction. What command would I use for this?

Have tried every combination of tags with these.

cmp file1 file2 | wc -l

Somehow I need to edit this to work right, not necessarily using the cmp command obviously.

like image 849
user2234688 Avatar asked Dec 12 '25 05:12

user2234688


2 Answers

I found that a side-by-side diff, suppressing context lines, is an effective method:

diff -y --suppress-common-lines file1 file2 | wc -l
like image 111
skiggety Avatar answered Dec 14 '25 03:12

skiggety


This should do what you want

diff -U 0 file1 file2 | grep -c ^@

For example file1 contains

aaa
bbb
ccc

file2 contains

aaa
ccc
ddd

Result:

 diff -U 0 file1 file2 | grep -c ^@
 2
like image 36
cmd Avatar answered Dec 14 '25 02:12

cmd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!