Sed replace not functioning as expected

5,346

Solution 1

First of all, A single quote may not occur between single quotes, even when preceded by a backslash., ref Bash Manual

Second, you may want to use some other char as separator instead of / as you have / in the replacement string.

So as a result:

sudo sed "s#listen = 127.0.0.1:9000#listen = '/var/run/php56-fpm.sock'#g" /etc/php-fpm.d/www.conf

don't use -i yet, have a look at the print out to verify if it works as expected.

Solution 2

try using other chars rather than / for separation maybe?

sudo sed -i "s@listen = 127.0.0.1:9000@listen = '/var/run/php56-fpm.sock'@g" /etc/php-fpm.d/www.conf

OR

sudo sed -i "s/listen = 127.0.0.1:9000/listen = '\/var\/run\/php56-fpm.sock'/g" /etc/php-fpm.d/www.conf

The problem is that you are not escaping the / as \/ but using @ as separator will fix your problem. You can use ANY separator in sed as soon as your are consistent.

And true from @David, use double quotes as per bash manual.

tip: all UX files need EOF in the last empty line

Share:
5,346

Related videos on Youtube

Adam Silver
Author by

Adam Silver

Updated on September 18, 2022

Comments

  • Adam Silver
    Adam Silver over 1 year

    I want to replace listen = 127.0.0.1:9000 by listen = '/var/run/php56-fpm.sock'

    sudo sed -i 's/listen = 127.0.0.1:9000/listen = \'/var/run/php56-fpm.sock\'/g' /etc/php-fpm.d/www.conf

    Error:

    bash: -c: line 63: unexpected EOF while looking for matching `''
    bash: -c: line 64: syntax error: unexpected end of file
    
  • Adam Silver
    Adam Silver about 8 years
    both are not working.
  • Kramer
    Kramer about 8 years
    sorry, I corrected, from single to double quote enclosing, David is right :)
  • Jason
    Jason about 8 years
    Indeed, a lot of people forget about being able to use a different separator character such as ':', which also holds true when doing search and replace in vim.