【问题标题】:Issue using regex option with 'diff'使用带有 'diff' 的正则表达式选项的问题
【发布时间】:2012-11-05 18:58:16
【问题描述】:

d我一直在尝试做与此非常相似的事情...Bash "diff" utility showing files as different when using a regex Ignore

并且已经做了几个小时但没有运气。

我确信 -I 选项不起作用。下面是使用另一个问题给出的示例进行的测试。

注意:我使用的是 RHEL6 附带的标准版本的 'diff',并且 -I 包含在手册页中,因此没有理由相信它不应该工作,但事实并非如此。任何帮助或指导将不胜感激

[toernerg@uschi12devwom27: ~] $ cat testfile1
// $Id: one
data

[toernerg@uschi12devwom27: ~] $ cat testfile2
// $Id: two
data

[toernerg@uschi12devwom27: ~] $ diff testfile1 testfile2
1c1
< // $Id: one
---
> // $Id: two

[toernerg@uschi12devwom27: ~] $ diff -I '.*\$\(Id\|Header\|Date\|DateTime\|Change\|File\|Revision\|Author\):.*\$.' testfile1 testfile2
1c1
< // $Id: one
---
> // $Id: two

[toernerg@uschi12devwom27: ~] $ diff --version
diff (GNU diffutils) 2.8.1

【问题讨论】:

  • 格式输出不正确。让我尝试粘贴而不格式化...
  • 您的示例输入文件在冒号后不包含文字 $。也许您不是要转义正则表达式中的$,而是想匹配行尾。

标签: regex shell diff


【解决方案1】:

您的正则表达式在两个地方需要美元符号,但您的输入只有第一个。修复它,它就可以工作了:

[user@host ~]$ cat testfile1
// $Id: one
data

[user@host ~]$ cat testfile2
// $Id: two
data

[user@host ~]$ diff -I '.*\$\(Id\|Header\|Date\|DateTime\|Change\|File\|Revision\|Author\):.*\$.' testfile1 testfile2
1c1
< // $Id: one
---
> // $Id: two
[user@host ~]$ diff -I '.*\$\(Id\|Header\|Date\|DateTime\|Change\|File\|Revision\|Author\):.*' testfile1 testfile2
[user@host ~]$ diff --version
diff (GNU diffutils) 2.8.1

(顺便说一句,实际上不需要前导和尾随.*。)

【讨论】:

    【解决方案2】:

    要在您的示例案例中工作,请尝试:

    diff -I '\$\(Id\|Header\|Date\|DateTime\|Change\|File\|Revision\|Author\):' 
    

    【讨论】:

    • uhhggg,好的,这对那个例子有用(我直接从另一个问题复制了它,但没有抓住第二个 $)。也就是说,关于真正的问题。
    • 这是一个新的真实示例,我的用例使用 xml 文件。也许与它是xml有关?并且读取不正确?
    • [toernerg@uschi12devwom27: ~] $ cat test1.xml w3.org/2000/10/XMLSchema-instance"> -- xml 数据在这里 -- [toernerg@ uschi12devwom27: ~] $ cat test2.xml w3.org/2000/10/XMLSchema-instance"> -- 这里是xml数据 --
    • [toernerg@uschi12devwom27: ~] $ diff -I '.*GENERATED.*' test1.xml test2.xml 3,4c3,4 --- > >
    • @gt:您可能应该编辑您最初的问题,而不是尝试在 cmets 中发布 XML 文件。 :-P
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多