我在 httpd.conf 文件中有以下虚拟主机
<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>
我正在尝试将“ServerName abc.com”添加到每个尚未设置 ServerName 的虚拟主机。
我试图在 sed 中完成它,但我没有取得任何进展......有什么帮助吗?
这就是我目前所拥有的...
sed '/^[ \t]*<VirtualHost/,/^[ \t]*<\/VirtualHost/{
/^ServerName/!{
/<VirtualHost/{
/^/a\ServerName abc.com
}
}
}' httpd.conf
最佳答案
使用 sed:
sed '/<VirtualHost/{
:a N;/<\/VirtualHost>/!b a;
/ServerName/!s!\(</VirtualHost>\)!ServerName abc.com\n\1!
}' input
我不认为 awk 在这个特定问题上比 sed 有任何优势。
关于linux - 使用 sed 将缺少的 ServerName 添加到 httpd.conf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15957706/