rename files by content of file
Hey All,
I have large apache.conf with 99 VirtualHosts and I want to break them out to single conf files for each VH and rename that new VH conf after the ServerName on line 4 of each file. looking for assistance in the scripting of this action? |
My approach would be to use perl's Config::ApacheFormat module to loop through your existing configuration file and separate out each virtual host and write them to separate files. ServerName or ServerAlias or another identifier might then be included in the file name.
|
never used perl's Config::ApacheFormat do you have any reference links so I can read up and learn how to use this?
|
Yes, the manual page linked to in the previous post has a full example. You can get the module from the package libconfig-apacheformat-perl on most Debian-based distros.
|
I used csplit to break out the file
csplit --digits=2 --quiet --prefix=outfile httpd.conf "/</VirtualHost>/+1" "{*}" this created outputfile##.conf then ran this to change the file names for i in *.conf ; do echo mv $i `egrep -m1 -e 'ServerName *' $i | sed -e 's/ServerName//'`; done that did it |
Very nice. I had not seen csplit before. I keep reaching for that perl hammer.
|
Always nice to see a new tool I have not played with :) I am curious how the regular expression knows to skip the second of the three /'s? As with sed, I would have thought it would hit the second
and either throw an issue or potentially match the wrong thing?? Also, assuming the split has worked correctly, why would you need to bail on the first match with grep? Wouldn't there only be a single servername in each file? |
All times are GMT -5. The time now is 06:43 PM. |