I've two configuration files, the original from the package manager and a customized one modified by myself. I've added some comments to describe behavior.
How can I run diff
on the configuration files, skipping the comments? A commented line is defined by:
- optional leading whitespace (tabs and spaces)
- hash sign (
#
) - anything other character
The (simplest) regular expression skipping the first requirement would be #.*
. I tried the --ignore-matching-lines=RE
(-I RE
) option of GNU diff 3.0, but I couldn't get it working with that RE. I also tried .*#.*
and .*\#.*
without luck. Literally putting the line (Port 631
) as RE
does not match anything, neither does it help to put the RE between slashes.
As suggested in “diff” tool's flavor of regex seems lacking?, I tried grep -G
:
grep -G '#.*' file
This seems to match the comments, but it does not work for diff -I '#.*' file1 file2
.
So, how should this option be used? How can I make diff
skip certain lines (in my case, comments)? Please do not suggest grep
ing the file and comparing the temporary files.
-I
option causes a block to be ignored only if all its lines match the regexp. So you can ignore a comment-only change that way, but not the comment changes that are near a non-comment change.diff -I
does not behave as I expected. I updated my answer with an example which clarified this behavior for me.