我正在自动化在我公司创建虚拟主机的过程,我想将这一行: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
下,如何实现?
答案 0 :(得分:1)
这个awk应该可以工作:
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>
答案 1 :(得分:1)
假设我们想在文档中的某个位置插入一行,那么你可以使用这样的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