我怎样才能忽略“diff”中的一些差异?命令?

发布于 2024-10-07 07:22:26 字数 293 浏览 0 评论 0原文

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

例如,对于给定的 XY,我想忽略 "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

誰ツ都不明白 2024-10-14 07:22:26

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

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

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:

diff <(sed '/abXd/,/abYd/d' file1) <(sed '/abXd/,/abYd/d' file2)
惟欲睡 2024-10-14 07:22:26

改进 John Kugelman 的早期解决方案

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

可能就是您所寻找的!此版本标准化了每行的特定更改,而不删除该行本身。这允许 diff 显示行中保留的任何其他差异。

Improving upon the earlier solution by 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 other differences that remain on the line.

扭转时空 2024-10-14 07:22:26

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

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

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

diff <(sed 's/ab[XY]d/ab__REPLACED__d/g' file1) <(sed 's/ab[XY]d/ab__REPLACED__d/g' file2)
别念他 2024-10-14 07:22:26

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

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

小霸王臭丫头 2024-10-14 07:22:26

我的开源 Linux 工具“dif”比较文件,同时忽略各种差异。

它有许多选项用于执行搜索/替换、忽略空格、注释或时间戳、对输入文件进行排序、忽略某些行等。

预处理输入文件后,它运行 Linux 工具 Meld、gvimdiff、tkdiff、diff 或 Kompare 这些中间文件。

不需要安装。只需从 dif 下载并运行“dif”可执行文件即可。

对于您的用例,请尝试搜索和替换选项:

./dif file1 file2 -search 'ab[XY]d' -replace 'abd' -diff

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:

./dif file1 file2 -search 'ab[XY]d' -replace 'abd' -diff
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文