我在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 '/^[ \t]*<VirtualHost/,/^[ \t]*<\/VirtualHost/{
/^ServerName/!{
   /<VirtualHost/{
     /^/a\ServerName abc.com

   }
  }
}' httpd.conf

最佳答案

我想也许awk更适合这个任务。这里是add-server-name.awk

/<VirtualHost/ { named=0; indent=substr($0,1,match($0,/<VirtualHost/)-1) }
/^[[:blank:]]*ServerName[[:blank:]]*/ { named=1; }
/<\/VirtualHost>/ { if (!named) print indent "ServerName abc.com" }
{ print }

用法:
awk -f add-server-name.awk httpd.conf

关于linux - 使用sed将缺少的ServerName添加到httpd.conf文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15957706/

10-12 05:01