Skip to content
Advertisement

Updating a yaml with a string containing special characters using sed

I have a yaml file that needs to be updated with string containing special characters. Here is the command I used but I get sed expression error

Yaml file (file):

Key1: 
Key2: 

Command that works without special characters for $var (env variable):

sed  -i '0,/^([[:space:]]*Key1: *).*/s//1'$var'/;' file

Value for $var:

fkugoiuhoiuyflkbbui/qy++bfv7J3c

Error I get is:

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

I am trying to figure out how I can get this working. Any help is greatly appreciated. Thanks!

Advertisement

Answer

The default delimiter of sed is conflicting with the / in your variable. You will need to set a different delimiter, further, the single quotes will not allow the variable to expand.

You can try this sed

$ sed "|Key1|s|$|'$var'|" input_file
Key1: 'fkugoiuhoiuyflkbbui/qy++bfv7J3c'
Key2:
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement