Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Hi I need to extract each VirtualHosts from my httpd.conf to separate files for easier management.

Original httpd.conf .

..
<VirtualHost *:XXXX>
  SuexecUserGroup user1 groupX
  ...
</VirtualHost>
<VirtualHost *:XXXY>
  SuexecUserGroup user2 groupY
  ...
</VirtualHost>
<VirtualHost *:XXYY>
  SuexecUserGroup user3 groupZ
  ...
</VirtualHost>
...

And I would like to have files like: - XXXX_user1.conf containing:

Listen XXXX
<VirtualHost *:XXXX>
  SuexecUserGroup user1 groupX
  ...
</VirtualHost>

- XXXY_user2.conf containing:

Listen XXXY
<VirtualHost *:XXXY>
  SuexecUserGroup user2 groupY
  ...
</VirtualHost>

Sounds a bit complicated but I have like 500 to do by hand ;(

Many thanks for answers

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Give this a try:

#!/usr/bin/gawk -f
BEGIN {
    RS = "</VirtualHost>\n"
}
{
    prefix = gensub("*:([^>]*)>", "\\1", "1", $2)
    filename = prefix "_" $4 ".conf"
    print "Listen " prefix "\n" $0 "\n" RS > filename
}

Save it in a file named, perhaps, "httpdsplit" and do:

$ chmod u+x httpdsplit
$ ./httpdsplit httpd.conf
share|improve this answer
    
Awesome!! Many many thanks! –  Warnaud Oct 5 '10 at 11:27
    
I've tweaked a bit the script so it adds also Listen XXXX at the beginning: . #!/usr/bin/gawk -f BEGIN { RS = "</VirtualHost>\n" } { prefix = gensub(":([^>])>", "\\1", "1", $2) filename = prefix "_" $4 ".conf" print "Listen "prefix $0 "" RS > filename } –  Warnaud Oct 5 '10 at 11:28
    
@Warnaud: Oops, sorry, I forgot to add that part. I'll edit my answer. Don't forget to mark it as accepted if it satisfies your requirement. –  Dennis Williamson Oct 5 '10 at 14:55
    
I would like to really since it's exactly what I was looking for but ... how to do that :( ? The site is great but the syntax and this kind of options are quite invisible. –  Warnaud Oct 6 '10 at 11:15
    
I found out ;-) Thanks again –  Warnaud Oct 6 '10 at 14:12

Though you didn't mention it, this looks like apache httpd.conf. Though I recommend the existing answer which splits the files, there is another option.

If the apache server is built with mod_perl and if you know (or are willing to learn) Perl, remember that mod_perl binds with all aspects of apache, including config. You could write the config with a Perl stanza and generate the VirtualHost config within perl itself.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.