Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Bash:如何在文件中的特定位置插入一行?_Linux_Apache_Bash - Fatal编程技术网

Linux Bash:如何在文件中的特定位置插入一行?

Linux Bash:如何在文件中的特定位置插入一行?,linux,apache,bash,Linux,Apache,Bash,我正在自动化公司中创建虚拟主机的过程,我想将以下行添加到名为sitename.com.conf的文件中:ServerAlias sitename.booking.local,如下所示: <VirtualHost *:80> ServerName www.sitename.com ServerAlias sitename.com ServerAlias sitename.booking.local DocumentRoot /site/http/travel/i

我正在自动化公司中创建虚拟主机的过程,我想将以下行添加到名为sitename.com.conf的文件中:
ServerAlias sitename.booking.local
,如下所示:

<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>

服务器名www.sitename.com
ServerAlias sitename.com
ServerAlias sitename.booking.local
DocumentRoot/site/http/travel/itn
自定义日志/访问\u站点名称\u日志组合
DirectoryIndex default.php index.php index.html index.phtml index.cgi index.htm
ScriptAlias/awstats//usr/local/awstats/wwwroot/cgi-bin/
允许超越所有
我希望该行位于
中,并将其添加到文件中最后一行
ServerAlias
下,如何实现该行?

此awk应工作:

awk  -v a="mysite" '/<VirtualHost /{v=1}
     v && /^ *ServerAlias/{{$0="   ServerAlias " a ".booking.local" RS $0; v=0} 
     /<\/VirtualHost>/{v=0} 1' file

awk-v a=“mysite”'/假设我们想在文档中的某个位置插入一行,那么您可以像这样使用sed添加到第四行:

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

是否有任何特定原因仅将其添加到最后一行
ServerAlias
下?不,这不是必须的,但它必须位于
内,而不是
内。服务器别名允许在同一行上有多个值。。不需要新的线路。。。sed“s/ServerAlias.*/&newvalue/”-i vhostfileBrenoZan:谢谢,你的评论解决了我的问题。sitename.booking.local实际上是从$site和.booking.local构建的,那么如果站点是一个变量,那么该行看起来会是什么样子呢?这样行吗
awk'/请参阅上面关于如何将动态sitename传递到变量
a
的更新。
sed -i 's/ServerName www.sitename.com/ServerName www.sitename.com\\n   ServerAlias sitename.booking.local/g' myfile.xml