Gentoo Wiki


Apache2 Series



Addons & Tunnels






If you have made special changes to a file, you may want to run a diff before overwriting the new file with the old one. If you have not made special changes, it's probably best not to move it.

cd /etc/apache2
mv -i conf/vhosts/* vhosts.d
mv -i conf/modules.d/* modules.d/
cat conf/apache2.conf conf/commonapache2.conf > httpd.old
cp httpd.conf httpd.conf.diffagainst
perl -pi*.backup -e "s/(^|\n)(([^#\n\"']|\".*?\"|'.*?')+){0,1}#.*(\n|$)/if(\$2){\"\$1\$2\n\";\
  }else{"\$1";}/mge" httpd.conf.diffagainst httpd.old
diff httpd.conf.diffagainst httpd.old

The perl command will make in-place-replacements, but make backups to httpd.conf.diffagianst.backup and httpd.old.backup with the contents which were in the files before we tried to get rid of the comments. Just for safety I suggest you copy httpd.conf to httpd.conf.diffagainst before you get rid of the comments in the new file (and thus makes it comparable with the httpd.old with no comments.) The perl command is provided as is, and if you find any error in it, please correct it (documentation below.)

Here I will try to defend the way I've crafted the regular expression. I've split it up with comment lines starting with ##. Note that " and $ is escaped because it is invoked from a shell with "" to allow '. For background information check out man perlre and perhaps

## Start search/replace regular expression which will replace lines with comments - others not touched
## Match start of string or newline : $1
## Keep any characters except from #, don't match newline
## anything within  or "" is kept as is (.*? make non-greedy searches) : $2
## Match start of comment to newline or end of string (. will not match newline without the s-switch)
## End search-pattern, start instructions for replacement of matches
if(\$2){\"\$1\$2\n\";} else {"\$1";}
## m: multiline, g:run as long as there are more matches and e:evaluate replacement as perl code

To be deleted in a month or two after official move.

Retrieved from ""

Last modified: Thu, 15 Dec 2005 00:03:00 +0000 Hits: 14,954