简体   繁体   中英

bash scripting: find and replace multiword string

I'm trying to write a bash script to configure a server and I need to change the line:

    listen = /var/run/php5-fpm.sock

to equal the following:

    listen = 127.0.0.1:9000

in the file:

    /etc/php5/fpm/pool.d/www.conf

So I've been looking at tutorials for using sed and I've tried the following command to no avail:

    $~: sed -i 's//var/run/php5-fpm.sock/127.0.0.1:9000/g' /etc/php5/fpm/pool.d/www.conf
    $~: sed: -e expression #1, char 8: unknown option to `s'

I've tried escaping the forward slash with a backslash: '/' but I think I'm on the wrong track. There must be a better way to do this?

Thanks for your help.

This is because you are trying to replace the character '/' in the pattern, and this character is used to delimit the 's///' expression. You have two choices, you can escape every '/' character with '/'or - and this is the one I prefer, use a different character to delimit the pattern and replacement string - I tend to use '!'

The character immediately after the 's' is used to delimit the expressions.

sed -i 's!/var/run/php5-fpm.sock!127.0.0.1:9000!g' /etc/php5/fpm/pool.d/www.conf

I actually have got into the habit of ALWAYS using '!' for sed, and perl - as you end up having to escape less characters and ultimately save time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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