我必须在特定的虚拟主机块中添加一行
<VirtualHost *:80>
#
#Existing Lines
#
#--New line should go here
</VirtualHost>
sed脚本应该不应该将此行添加到任何其他虚拟主机块。
答案 0 :(得分:0)
<VirtualHost url="server:port">
如果满足以上条件,请检查以下内容:
kent$ cat vh.txt
<VirtualHost url="bar:80">
<bar>i am virtualHost bar</bar>
</VirtualHost>
<VirtualHost url="here:80">
#
#Existing Lines
#
</VirtualHost>
<VirtualHost url="foo:80">
<foo>i am fooo</foo>
</VirtualHost>
kent$ awk '/<VirtualHost url="here:80">/&& !x{x=1;print;next;}/<\/VirtualHost>/&&x==1{$0="NEW_NEW\n"$0;x=2}1' vh.txt
<VirtualHost url="bar:80">
<bar>i am virtualHost bar</bar>
</VirtualHost>
<VirtualHost url="here:80">
#
#Existing Lines
#
NEW_NEW
</VirtualHost>
<VirtualHost url="foo:80">
<foo>i am fooo</foo>
</VirtualHost>
上面的awk在线人员在新的VirtualHost元素中添加了一行“NEW_NEW”。
答案 1 :(得分:0)
使用sed
的一种方法。它选择<VirtualHost *:80>
和</VirtualHost>
之间的行,并在最后一行中插入文本。
sed -e '/^<VirtualHost[ ]\*:80>/,/<\/VirtualHost>/ { /<\/VirtualHost>/ i\NEW TEXT' -e '}'
示例:
假设infile
的下一个内容:
<VirtualHost more:80>
#
#Existing Lines
#
#--New line should go here
</VirtualHost>
<VirtualHost *:80>
#
#Existing Lines
#
#--New line should go here
</VirtualHost>
<VirtualHost *:8080>
#
#Existing Lines
#
#--New line should go here
</VirtualHost>
像以下一样运行:
sed -e '/^<VirtualHost[ ]\*:80>/,/<\/VirtualHost>/ { /<\/VirtualHost>/ i\NEW TEXT' -e '}' infile
使用以下输出:
<VirtualHost more:80>
#
#Existing Lines
#
#--New line should go here
</VirtualHost>
<VirtualHost *:80>
#
#Existing Lines
#
#--New line should go here
NEW TEXT
</VirtualHost>
<VirtualHost *:8080>
#
#Existing Lines
#
#--New line should go here
</VirtualHost>