Sed replace strings starts with special characters

Viewed 68

I'm trying to replace strings with sed in ; php_value[date.timezone] = Europe/Riga

i tried something like this:

sed -i 's/; php_value[date.timezone] = Europe/\Riga/; php_value[date.timezone] = America/\Sao_Paulo/g' file

Output: sed: -e expression #1, char 47: extra characters after command

1 Answers

You can use

sed -i 's/; php_value\[date\.timezone] = Europe\/Riga/; php_value[date.timezone] = America\/Sao_Paulo/g' file

See the online demo.

NOTE:

  • [ and . are special regex metacharacters and need to be escaped to match literal [ and ., hence, \[ and \. in the regex part
  • / is a regex delimiter char here, and should also be escaped. To escape /, use \/. Well, if you use another regex delimiter char, you will have no need escaping /, e.g.
sed -i 's,; php_value\[date\.timezone] = Europe/Riga,; php_value[date.timezone] = America/Sao_Paulo,g' file

See the commas as regex delimiters here.

Related