简体   繁体   English

Bash:在文件中查找单词并在其后添加文本

[英]Bash: Find a word in file and add text after it

I'm trying to build my own bash script for installing LEMP (nginx mariadb php).我正在尝试构建自己的 bash 脚本来安装 LEMP(nginx mariadb php)。 I've managed to pull it off but I got stuck at the part where I need to add the 'index.php' text to the index line @ /etc/nginx/sites-avaliable/default.我已经设法把它拉下来,但我被困在我需要将“index.php”文本添加到索引行@/etc/nginx/sites-avaliable/default 的部分。

I've tried with "sed" and got very close but not there yet.我试过“sed”并且非常接近,但还没有。

sed '/\index.htm/i index.php' input /etc/nginx/sites-available/default

that was the closest I got那是我得到的最接近的

i also tried replacing the index.htm file like this :我也尝试像这样替换 index.htm 文件:

sed 's/index.htm/index.php' /etc/nginx/sites-available/default

Thanks for the help!谢谢您的帮助! :) :)

If you want to replace in the input file, you have to use the -i option.如果要在输入文件中进行替换,则必须使用-i选项。 Otherwise sed sends the output to stdout.否则sed将输出发送到 stdout。

The s command requires a terminating delimiter. s命令需要一个终止分隔符。

sed -i 's/index.html/index.html index.php/' /etc/nginx/sites-available/default

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM