Can I ignore comments on diff using regular expressions?

I have to do a file comparison but I want you to exclude the comments for now it gives me a result like this.

At the moment I am using: diff -b -B [patch] [patch] which gives me:

< #<Location /bancochile>
< #        WeblogicHost bgri.wls.ri
< #        WeblogicPort 20015
< #        SetHandler weblogic-handler
< #</Location>
< 
45c39,43

Upvotes: 3

Views: 338

Answers (2)

Charles Duffy
Charles Duffy

Reputation: 295443

As suggested by @chepner, GNU diff has support explicitly for this purpose:

diff -I '^[[:space:]]*[#]' -b -B old.cfg new.cfg

That said, if you only want to know if there are or are not changes, it's much more efficient to use cmp:

if cmp -s <(grep -E -v '^[[:space:]]*[#]' <old.cfg) \
          <(grep -E -v '^[[:space:]]*[#]' <new.cfg); then
  echo "Excluding comments, these files are identical"
fi

Upvotes: 2

Cyrus
Cyrus

Reputation: 88646

I suggest with bash:

diff -b -B <(grep -v '^#' patch1) <(grep -v '^#' patch2)

Upvotes: 2

Related Questions