• Uncategorized

About linux : Can-I-ignore-comments-on-diff-using-regular-expressions

Question Detail

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
which gives me:

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

Question Answer

I suggest with bash:

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

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

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.