Bash“差异”使用正则表达式忽略时将文件显示为不同的实用程序
我正在尝试使用此处记录的 bash 实用程序“diff”: http://ss64.com/ bash/diff.html。请注意,我使用的是 bash 实用程序的 Windows 移植版本,但这应该没有任何区别。
我有两个文件 regex_test_1.txt 和 regex_test_2.txt,它们具有以下内容:
regex_test_1.txt:
// $Id: some random id string $ more text
text that matches
regex_test_2.txt:
// $Id: some more random id string $ more text
text that matches
我试图比较这些文件,同时忽略适合此正则表达式的任何行:
.*\$(Id|Header|Date|DateTime|Change|File|Revision|Author):.*\$.*
但是,当我运行 diff 并告诉它使用 -I 参数忽略与此正则表达式匹配的行,这是输出:
C:\Users\myname\Documents>diff -q -r -I ".*\$(Id|Header|Date|DateTime|Change|File|Revision|Author):.*\$.*" regex_test_1.txt regex_test_2.txt
Files regex_test_1.txt and regex_test_2.txt differ
我希望它应该没有发现任何差异(并且不报告任何内容)。为什么发现这些文件不同?
I'm trying to use the bash utility "diff" that is documented here: http://ss64.com/bash/diff.html. Note that I'm using a windows-ported version of the bash utility, but that shouldn't make any difference.
I have two files, regex_test_1.txt and regex_test_2.txt that have the following contents:
regex_test_1.txt:
// $Id: some random id string $ more text
text that matches
regex_test_2.txt:
// $Id: some more random id string $ more text
text that matches
I am trying to diff these files while ignoring any lines that fit this regex:
.*\$(Id|Header|Date|DateTime|Change|File|Revision|Author):.*\$.*
However, when I run diff and tell it to ignore lines matching this regex using the -I argument, this is the output:
C:\Users\myname\Documents>diff -q -r -I ".*\$(Id|Header|Date|DateTime|Change|File|Revision|Author):.*\$.*" regex_test_1.txt regex_test_2.txt
Files regex_test_1.txt and regex_test_2.txt differ
I expect that it should find no differences (and report nothing). Why is it finding these files to be different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为
diff
使用基本的正则表达式语法,其中某些正则表达式元字符失去了其特殊含义:这应该有效:
It's because
diff
uses basic regex syntax, wherein certain regex metacharacters lose their special meaning:This should work:
只是为了咯咯笑,将 -b 添加到您的 diff 中。忽略空白区域的差异。
Just for giggles, add -b to your diff. Ignore differences in white space.