I have several vhosts in one file that I am looking to break each into a file of their own corresponding to the ServerName
. I'm looking to take this file
<VirtualHost *:80>
ServerName host1.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName host2.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName hostN.example.com
</VirtualHost>
to subfiles host1.example.com
... hostN.example.com
.
I looked at this question-answer, which is closest to what I'm looking for, but I'm unsure on how to match two lines. It tried a nested if statement,
gawk '{
if(match($0, /(\<VirtualHost \*:80\>)/, v)){
if(match($0, / ServerName (.*)/, s)){
name=s[1]
{print name}
}}}
' samplevhost
and
gawk '{
if(match($0, /(\<VirtualHost \*:80\>)(\n )(ServerName)(.*)/, k)){
name=k[1]
{print name}
}}
' samplevhost
to print to screen, but I don't get any output. What am I missing?
<VirtualHost>
container or not, then check that state variable to determine whether to do any further processing (e.g. to look for aServerName
directive). Remember that there's no syntax guarantee that ServerName comes first in the VirtualHost container.