diff command to get number of different lines only

半世苍凉 提交于 2020-08-17 23:48:20

问题


Can I use the diff command to find out if two of files differ by k lines?

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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/27236891/diff-command-to-get-number-of-different-lines-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!