Reputation: 55
I am trying to write a script that configures a config file used by a nother script. I am trying to use sed like this
sed -c -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIG_FILE
But it's not working as it is intended to it strips the quotation marks and i cant figure out how to write it so it dont.
the second problem is that when i run this on Mac OS the out put is an error:
sed: illegal option -- c
usage: sed script [-Ealn] [-i extension] [file ...]
sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
I am new to usage of sed so please forgive my lack of skills in this area.
Upvotes: 4
Views: 10033
Reputation: 195059
see the test below, I didn't add "-i", just print the output. you can add -i if you need:
kent$ cat c.conf
key1="value1"
foo = "fooValue"
bar="barValue"
kent$ echo $k1
foo
kent$ echo $v1
foo_new
kent$ sed -r "s/($k1 *= *\").*/\1$v1\"/" c.conf
key1="value1"
foo = "foo_new"
bar="barValue"
Upvotes: 2
Reputation: 8623
Have you tried escaping the quotes? This works for me (on Cygwin):
~$ echo -e "key1=\"value1\"\nkey2=\"value2\""
key1="value1"
key2="value2"
~$ TARGET_KEY=key2
~$ REPLACEMENT_VALUE=new_val
~$ echo -e "key1=\"value1\"\nkey2=\"value2\"" | sed "s/\($TARGET_KEY *= *\"\).*/\1$REPLACEMENT_VALUE\"/"
key1="value1"
key2="new_val"
~$
Upvotes: 0