【问题标题】:How to diff between 2 files with ignore-matching-lines option with diff command如何使用 diff 命令使用忽略匹配行选项在 2 个文件之间进行差异
【发布时间】:2021-06-28 21:25:21
【问题描述】:

我有 2 个文件,其中有一个与另一个文件不匹配的特定字符串。我需要使用diff -I RE <file1> <file2> 命令忽略该差异。

我从man diff找到这个

   -I RE  --ignore-matching-lines=RE
          Ignore changes whose lines all match RE.

$cat 1.txt

MY_WORD
temp
abc

$cat 2.txt

YOUR_WORD
temp
abc

$diff -I MY_WORD 1.txt 2.txt

1c1
< MY_WORD
---
> YOUR_WORD
4d3
< 

谁能帮我弄清楚为什么这不起作用? diff 不应在此处显示任何更改。

【问题讨论】:

标签: linux bash shell diff


【解决方案1】:

试试

diff -I ".*_WORD" 1.txt 2.txt

可惜diff似乎只懂基本的RE,所以首选

diff -I "(MY|YOUR)_WORD" 1.txt 2.txt

没用=(

编辑: 这里是我从你的样本和我的调用中得到的(以及一个不匹配的 RE - 没有前导空格):

cat 1.txt 2.txt 
MY_WORD
temp
abc
YOUR_WORD
temp
abc
diff -I ".*_WORD" 1.txt 2.txt
diff -I " .*_WORD" 1.txt 2.txt
1c1
< MY_WORD
---
> YOUR_WORD

【讨论】:

  • 它仍然返回4d3 &lt; 输出
  • 这意味着您的一个 ACTUAL 文件有一个空行,而另一个没有。
猜你喜欢
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 2017-04-06
  • 1970-01-01
  • 2015-10-20
  • 2012-09-25
  • 1970-01-01
相关资源
最近更新 更多