json - Replace text in a variable by another variable -


i want replace text in variable variable.

body='{ "server": {     "metadata": "metatoreplace"   } }' meta="{   arts_oracle_int_ip: 10.120.47.151,   arts_user: performance }" 

i tried this, didn't work:

body=$(echo "${body}" | sed "s|\"metatoreplace\"|${meta}|g") 

i got error :

sed: -e expression #1, char 19: unterminated `s' command 

the newlines in replacement variable wrecking syntax of s/// command:

$ echo "${body}" | sed "s|\"metatoreplace\"|${meta}|g" sed: -e expression #1, char 19: unterminated `s' command 

i'd use awk: can transfer contents of shell variable awk variable:

body=$( awk -v rep="$meta" '{gsub(/"metatoreplace"/, rep); print}' <<< "$body" ) 

Comments