我怎样才能忽略“diff”中的一些差异?命令?
diff
有一个选项 -I regexp
,它会忽略仅插入或删除与给定正则表达式匹配的行的更改。当更改发生在两行之间(而不是插入或删除行)时,我需要一个类似的情况。
例如,对于给定的 X
和 Y
,我想忽略 "abXd"
和 "abYd"
之间的所有差异代码>.
看来 diff
没有任何这样的能力。 diff
是否有合适的替代方案?
diff
has an option, -I regexp
, which ignores changes that just insert or delete lines that match the given regular expression. I need an analogue of this for the case, when changes are between two lines (rather than insert or delete lines).
For instance, I want to ignore all differences like between "abXd"
and "abYd"
, for given X
and Y
.
It seems diff
doesn't have any such kind of ability. Is there a suitable alternative for diff
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过 sed 过滤这两个文件以消除您不关心的行。一般模式是
/regex1/,/regex2/ d
用于删除与两个正则表达式匹配的行之间的任何内容。例如: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:改进 John Kugelman 的早期解决方案:
可能就是您所寻找的!此版本标准化了每行的特定更改,而不删除该行本身。这允许 diff 显示行中保留的任何其他差异。
Improving upon the earlier solution by John Kugelman:
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 other differences that remain on the line.
您可以使用 sed 将模式实例替换为标准字符串:
You could use sed to replace instances of the pattern with a standard string:
假设 X 和 Y 是单个字符,那么
-I 'ab[XY]d'
对我来说效果很好。Assuming X and Y are single characters, then
-I 'ab[XY]d'
works fine for me.我的开源 Linux 工具“dif”比较文件,同时忽略各种差异。
它有许多选项用于执行搜索/替换、忽略空格、注释或时间戳、对输入文件进行排序、忽略某些行等。
预处理输入文件后,它运行 Linux 工具 Meld、gvimdiff、tkdiff、diff 或 Kompare 这些中间文件。
不需要安装。只需从 dif 下载并运行“dif”可执行文件即可。
对于您的用例,请尝试搜索和替换选项:
My open source Linux tool 'dif' compares files while ignoring various differences.
It has many options for doing search/replace, ignoring whitespace, comments, or timestamps, sorting the input files, ignoring certain lines, etc.
After preprocessing the input files, it runs the Linux tools Meld, gvimdiff, tkdiff, diff, or Kompare on these intermediate files.
Installation is not required. Just download and run the 'dif' executable from dif.
For your use case, try the search and replace option: