diff 命令仅获取不同行的数量 [英] diff command to get number of different lines only

查看:41
本文介绍了diff 命令仅获取不同行的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 diff 命令找出两个文件是否相差 k 行吗?

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.

推荐答案

是的,您可以,在真正的 Linux 方式中,您可以使用通过管道连接在一起的许多命令来执行任务.

Yes you can, and in true Linux fashion you can use a number of commands piped together to perform the task.

首先您需要使用 diff 命令来获取文件中的差异.

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

您可以使用 grep 工具按如下方式过滤掉这些内容

You use the grep tool to filter these out as follows

diff file1 file2 | grep "^>"

最后,一旦您获得了您感兴趣的更改列表,您只需在行模式下使用 wc 命令来计算更改的数量.

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

你有一个完美的例子来说明 Linux 的哲学.

and you have a perfect example of the philosophy that Linux is all about.

这篇关于diff 命令仅获取不同行的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆