Consider the need to use 'diff' to compare two files with many commented lines, such as system configuration files. (For example, the files could be /etc/cups/cups-files.conf.pacnew and /etc/cups/cups-files.conf.)
I would like to ignore differences of any lines that are commented out such as these:
< #DocumentRoot /usr/share/cups/doc
---
> #DocumentRoot /usr/share/cups/doc-root
I only want to see differences of active (uncommented) lines.
(I would also like to ignore whitespace differences. I mention that for completeness in case the solution for the above interferes with using the the -w
option.)
1 Answer 1
You can filter the commented lines before comparing the files:
diff -u -w <(grep -v '^[[:space:]]*#' /etc/cups/cups-files.conf.pacnew) <(grep -v '^[[:space:]]*#' /etc/cups/cups-files.conf)
That way diff
will only see uncommented lines, and will only compare those.
-
can this be easily modified to work when the comment symbol is not in column 0? (That would be true in the case of leading whitespace.)MountainX– MountainX2018年02月17日 22:10:24 +00:00Commented Feb 17, 2018 at 22:10
-
This does not account for lines that may have comments at the end of them. A line that contains
DocumentRoot /usr/share/cups/doc #some comment here
would be excluded, regardless of whether the "active" part is different between files.Hiko Haieto– Hiko Haieto2018年02月17日 22:11:33 +00:00Commented Feb 17, 2018 at 22:11 -
@MountainX see the update.Stephen Kitt– Stephen Kitt2018年02月17日 22:12:17 +00:00Commented Feb 17, 2018 at 22:12
-
@Hiko no, it only excludes lines consisting solely of a comment.Stephen Kitt– Stephen Kitt2018年02月17日 22:12:37 +00:00Commented Feb 17, 2018 at 22:12
-
@HikoHaieto: Default config files are almost never commented in that manner.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2018年02月17日 22:12:48 +00:00Commented Feb 17, 2018 at 22:12
-I
?-I
only works if you have no mixed hunks – e.g. if you use-I '^#'
, but add one commented line followed by a non-commented line, you’ll see both in thediff
output because they’re part of the same hunk.