0

I'm trying to use sed to replace 'version' => '1.2.3', with 'version' => '1.2.4',.

Here's what I've tried:

echo " 'version' => '1.2.3', " | sed -E 's/([\s]*[\"\']version[\"\'][\s]*=>[\s]*[\"\'])[-_a-zA-Z0-9\.]+([\"\'][,]?)/\11.2.4\2/'

Here's what my shell tells me:

zsh: parse error near `)'

I've tried the same regex (minus the replacement part) on some online regex testers and it seems to work there. So why doesn't it work when I use it with sed?

Lars Nyström
  • 5,786
  • 3
  • 32
  • 33
  • Maybe it is a dupe of http://stackoverflow.com/questions/13799789/expansion-of-variable-inside-single-quotes-in-a-command-in-bash-shell-script – Wiktor Stribiżew Jul 13 '16 at 10:10

1 Answers1

2

You can't backslash a single quote in single quotes. In zsh, you can use '' to put a single quote into single quotes, or use the more portable '\'' (tested in both bash and zsh):

echo " 'version' => '1.2.3', " \
| sed -E 's/(\s*[\"'\'']version[\"'\'']\s*=>\s*[\"'\''])[-_a-zA-Z0-9\.]+([\"'\''],?)/\11.2.4\2/'

I also changed [\s] into \s and [,] into , as they are equivalent but the latter is simpler.

choroba
  • 231,213
  • 25
  • 204
  • 289