巴什" DIFF"使用正则表达式时,忽略实用的文件显示不同 [英] Bash "diff" utility showing files as different when using a regex Ignore

查看:122
本文介绍了巴什" DIFF"使用正则表达式时,忽略实用的文件显示不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此处介绍的是bash工具差别:的http:// ss64 .COM /庆典/ diff.html 。请注意,我用的是bash实用的Windows的移植版本,但不应该作出任何区别。

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.

我有两个文件,​​regex_test_1.txt和regex_test_2.txt具有以下内容:

I have two files, regex_test_1.txt and regex_test_2.txt that have the following contents:

regex_test_1.txt:

regex_test_1.txt:

// $Id: some random id string $ more text

text that matches

regex_test_2.txt:

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):.*\$.*

然而,当我运行差异,并告诉它忽略匹配使用-i参数此正则表达式的线条,这是输出:

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?

推荐答案

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

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

在基本的正前pressions的元字符?+,{,|,(和)失去了它们的特殊意义;改用反斜杠的版本 \\?\\ + \\ {,\\ |,\\(和\\)

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

这应该工作:

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

这篇关于巴什" DIFF"使用正则表达式时,忽略实用的文件显示不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆