Home > Software engineering >  Updating a yaml with a string containing special characters using sed
Updating a yaml with a string containing special characters using sed

Time:05-12

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!

CodePudding user response:

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:

CodePudding user response:

This is an issue that can happen no matter what delimiter of s/// you pick. The proper solution is to handle whatever delimiter characters appear in the data

escaped=${var//\//\\\/}    # escape all slashes
sed  -i '0,/^\([[:space:]]*Key1: *\).*/s//\1'"${escaped}"'/;' file

Breaking that down:

  • this uses the ${parameter/pattern/string} "search and replace" form of Shell Parameter Expansion
  • if the pattern starts with a /, then it's a global search and replace
  • the pattern is \/ which is a single forward slash: it needs to be escaped so that it is not interpreted as the separator between the pattern and the string.
  • then comes the / actually separating the pattern and the replacement string
  • the replacement string is \\\/: \\ is a literal backslash (that needs to go into the sed command) and lastly \/ is a literal slash.

With , a YAML processing tool, we can write

yq -i eval '.Key1 = "'"$var"'"' file

This is using the "go" implementaton of yq

$ yq --version
yq (https://github.com/mikefarah/yq/) version 4.25.1
  • Related