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
How to ignore some differences in diff command?
提问by Vahagn
diff
has 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 X
and Y
.
举例来说,我想忽略像之间的所有差异"abXd"
和"abYd"
,对于给定的X
和Y
。
It seems diff
has not such kind of ability. Is there any suitable alternative for diff
?
好像diff
没有这种能力。有没有合适的替代品diff
?
采纳答案by John Kugelman
You could filter the two files through sed
to eliminate the lines you don't care about. The general pattern is /regex1/,/regex2/ d
to 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)