3

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.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
user2234688
  • 79
  • 1
  • 5

2 Answers2

3

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

diff -y --suppress-common-lines file1 file2 | wc -l
skiggety
  • 205
  • 2
  • 7
1

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
cmd
  • 11,622
  • 7
  • 51
  • 61
  • 1
    It looks to me like this would count the number of differences, even if some of them are multiline, not the lines of difference. – skiggety Mar 07 '19 at 23:31