The httpd.conf exploder!

Posted on November 30, 2006

I thought that it’d be easy to break up a httpd.conf file up into multiple files per VirtualHost using a single line of perl. Turns out, I was most mistaken. Here is the one loooong line of perl to perform the explosion.

perl -ne ' if (/<Virtualhost (\*:80|[0-9.:]+)>/ .. /< \/VirtualHost>/) { $data.=$_ }; if (/ServerName/) { chomp; $sn=$_; $sn=~s/\s*ServerName\s*([^\s]+)\s*/$1/; } if (/<\/VirtualHost>/) { open(outfile, ">>$sn.conf"); print outfile $data; close outfile; $data="" } ' < httpd.conf