Bash“差异”使用正则表达式忽略时将文件显示为不同的实用程序

发布于 2024-12-05 18:30:33 字数 916 浏览 4 评论 0原文

我正在尝试使用此处记录的 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 技术交流群。

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

发布评论

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

评论(2

我最亲爱的 2024-12-12 18:30:33

这是因为 diff 使用基本的正则表达式语法,其中某些正则表达式元字符失去了其特殊含义:

在基本正则表达式中,元字符?、+、{、|、( 和 ) 失去了它们的特殊含义;而是使用反斜杠版本 \?、\+、\{、\|、\( 和 \)

这应该有效:

.*\$\(Id\|Header\|Date\|DateTime\|Change\|File\|Revision\|Author\):.*\$.*

It's because diff uses basic regex syntax, wherein certain regex metacharacters lose their special meaning:

In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).

This should work:

.*\$\(Id\|Header\|Date\|DateTime\|Change\|File\|Revision\|Author\):.*\$.*
破晓 2024-12-12 18:30:33

只是为了咯咯笑,将 -b 添加到您的 diff 中。忽略空白区域的差异。

Just for giggles, add -b to your diff. Ignore differences in white space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文