我正在自动化在我的公司中创建虚拟主机的过程,我想添加以下行:ServerAlias sitename.booking.local
到一个名为 sitename.com.conf 的文件中,如下所示:
<VirtualHost *:80>
ServerName www.sitename.com
ServerAlias sitename.com
ServerAlias sitename.booking.local
DocumentRoot /site/http/travel/itn
CustomLog logs/access_sitename_log combined
DirectoryIndex default.php index.php index.html index.phtml index.cgi index.htm
ScriptAlias /awstats/ /usr/local/awstats/wwwroot/cgi-bin/
<Directory /site/http/travel/itn >
AllowOverride All
</Directory>
</VirtualHost>
我希望该行在<virtualhost></virtualhost>
内并将其添加到文件中的最后一行ServerAlias
下,如何实现?
这个尴尬应该有效:
awk -v a="mysite" '/<VirtualHost /{v=1}
v && /^ *ServerAlias/{{$0=" ServerAlias " a ".booking.local" RS $0; v=0}
/</VirtualHost>/{v=0} 1' file
我们的产品:
<VirtualHost *:80>
ServerName www.sitename.com
ServerAlias sitename.booking.local
ServerAlias sitename.com
DocumentRoot /site/http/travel/itn
CustomLog logs/access_sitename_log combined
DirectoryIndex default.php index.php index.html index.phtml index.cgi index.htm
ScriptAlias /awstats/ /usr/local/awstats/wwwroot/cgi-bin/
<Directory /site/http/travel/itn >
AllowOverride All
</Directory>
</VirtualHost>
假设我们想在文档中的某个位置插入一行,那么您可以使用这样的 sed 添加到第 4 行:
sed '4a ServerAlias sitename.booking.local' myfile.xml > myfile.xml
这显然是危险的,如果文件结构将来可以更改,让我们在ServerName之后添加这一行,所以它会像
sed -i 's/ServerName www.sitename.com/ServerName www.sitename.com\n ServerAlias sitename.booking.local/g' myfile.xml