Linux 如何忽略 diff 命令中的一些差异?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4434346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:17:59  来源:igfitidea点击:

How to ignore some differences in diff command?

linuxbashdiffvimdiff

提问by Vahagn

diffhas an option -I regexp, which ignores changes that just insert or delete lines that match the given regexp. I need an analogue of this for the case, when changes are between two lines (rather then insert or delete lines).

diff有一个选项-I regexp,它忽略仅插入或删除与给定正则表达式匹配的行的更改。对于这种情况,当更改在两行之间(而不是插入或删除行)时,我需要一个类似的例子。

For instance, I want to ignore all differences like between "abXd"and "abYd", for given Xand Y.

举例来说,我想忽略像之间的所有差异"abXd""abYd",对于给定的XY

It seems diffhas not such kind of ability. Is there any suitable alternative for diff?

好像diff没有这种能力。有没有合适的替代品diff

采纳答案by John Kugelman

You could filter the two files through sedto eliminate the lines you don't care about. The general pattern is /regex1/,/regex2/ dto delete anything between lines matching two regexes. For example:

您可以过滤这两个文件sed以消除您不关心的行。一般模式是/regex1/,/regex2/ d删除匹配两个正则表达式的行之间的任何内容。例如:

diff <(sed '/abXd/,/abYd/d' file1) <(sed '/abXd/,/abYd/d' file2)

回答by Martin v. L?wis

Assuming X and Y are single characters, then -I 'ab[XY]d'works fine for me.

假设 X 和 Y 是单个字符,那么-I 'ab[XY]d'对我来说效果很好。

回答by Josy P. Pullockara

Improving upon the earlier solutionby John Kugelman:

改进John Kugelman的早期解决方案

diff <(sed 's/ab[XY]d/abd/g' file1) <(sed 's/ab[XY]d/abd/g' file2)

is probably what you may be looking for! This version normalizes the specific change on each line without deleting the line itself. This allows diff to show any otherdifferences that remain on the line.

可能是您正在寻找的!此版本规范了每一行上的特定更改,而不删除该行本身。这允许 diff 显示保留在行上的任何其他差异。

回答by slim

You could use sed to replace instances of the pattern with a standard string:

您可以使用 sed 用标准字符串替换模式的实例:

diff <(sed 's/ab[XY]d/ab__REPLACED__d/g' file1) <(sed 's/ab[XY]d/ab__REPLACED__d/g' file2)