开发者

Bash "diff" utility showing files as different when using a regex Ignore

开发者 https://www.devze.com 2023-04-07 00:20 出处:网络
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 dif

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 开发者_如何学Golines 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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消