bashsedmanipulate

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

I've tried with "sed" and got very close but not there yet.

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 :

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

Thanks for the help! :)


Solution

  • If you want to replace in the input file, you have to use the -i option. Otherwise sed sends the output to stdout.

    The s command requires a terminating delimiter.

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