Sed : unknown option to `s'

Im trying to change a config file by using bash script. Im using sed command to change as below.

VAR1=$(cat postgresql.conf | grep "shared_buffers =" | cut -d# -f1)
VAR2=$(shared_buffers = 8GB)
sed -ie s/${VAR1}/${VAR2}/g postgresql.conf 

I tried but it gave me error.

sed: -e expression #1, char 29: unknown option to `s'

What is missing?

5

1 Answer

The usual cause is that one (or both) of your variables contains the delimiter (in your case /). You can

  • change the delimiter if you have an alternative character that won't be present:

    sed -e "s#${VAR1}#${VAR2}#g"

    or

  • replace the delimiter in the variables:

    sed -e "s/${VAR1//\//\\/}/${VAR2//\//\\/}/g"

There may be other replacements you also need to make in $VAR1 if you want it to match as a literal, as s/// always uses regular expressions.


However, looking at what you have shown us, it appears that all you're really trying to do is change the value assigned to shared_buffers in the config file to the fixed value8GB, in which case there's no need for any variables:

sed -i -e '/^shared_buffers[ \t]/s/=[ \t]*[^# \t]\+/= 8GB/' /postgresql.conf

You can read that as in every line that begins with shared_buffers and a horizontal space, then replace the first = and the word that follows with = 8GB, which is what you seem to want.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like