Bash "diff" utility showing files as different when using a regex Ignore
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.
精彩评论