10

I'm trying to use sed to update a config file using a bash script. I have a similar sed command right above this one in the script that runs fine. I can't seem to figure out why this is breaking:

sed -i.bak \
    -e s/"socketPath:'https://localhost:9091'"/"socketPath:'/socket'"/g \
    $WEB_CONF

Any ideas?

Alexander Rolek
  • 933
  • 2
  • 12
  • 18

3 Answers3

26

The quotes and double quotes are causing problems. You are using them in view of the slashes in the string. In sed you can use another delimiter, such as a #.

sed -e 's#socketPath:https://localhost:9091#socketPath:/socket#g' \
$WEB_CONF
Walter A
  • 19,067
  • 2
  • 23
  • 43
8

Escape your slashes in the pattern or use a different delimiter like this:

sed -i.bak \
-e s%"socketPath:'https://localhost:9091'"%"socketPath:'/socket'"%g \
$WEB_CONF
aragaer
  • 17,238
  • 6
  • 47
  • 49
3

I am confused looking at the delimiters you have used so you can't blame sed for goofing up. When your dataset has / in them, it is often advised to use a different delimiters like # or _ (yes, sed supports various delimiters).

Also, your quoting looks abit off. The syntax should be:

sed -i.bak 's#substitute#replacement#g' "$WEB_CONF"

If your substitute and/or replacement has variables use " instead ' which will allow it to interpolate.

jaypal singh
  • 74,723
  • 23
  • 102
  • 147