I have the following virtual hosts in httpd.conf file
<VirtualHost *:80>
## could be comments
could be any line
could be any line
could be any line
</VirtualHost>
<VirtualHost *:80>
could be any line
## could be comments
could be any line
could be any line
could be any line
could be any line
could be any line
</VirtualHost>
<VirtualHost *:80>
## may have comments
ServerName ppp.com
could be any line
could be any line
could be any line
</VirtualHost>
<VirtualHost *:443>
## could be empty
ServerName zzz.com
could be any line
could be any line
could be any line
</VirtualHost>
I'm trying to add "ServerName abc.com" to every virtual host which doesn't have a ServerName already setup.
I was trying to do it in sed, and I'm not getting anywhere .. any help ?
This is what I have so far ...
sed '/^[ \t]*<VirtualHost/,/^[ \t]*<\/VirtualHost/{
/^ServerName/!{
/<VirtualHost/{
/^/a\ServerName abc.com
}
}
}' httpd.conf
Using sed:
sed '/<VirtualHost/{
:a N;/<\/VirtualHost>/!b a;
/ServerName/!s!\(</VirtualHost>\)!ServerName abc.com\n\1!
}' input
I don't think awk has any advantages over sed for this particular problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With