1

I have an object string in {"name": "VAULT", "value": "jsakjkaj"},{"name": "IMAGE", "value": "busybox"} format in a file called ENV.json.

i am assigning the content of ENV.json to a variable called VAL like below.

VAL=`cat ENV.json`

and i have a placeholder %KEYVALUES% in another file test.json . i want to replace %KEYVALUES% with $VAL like below

sed 's/%KEYVALUES%/'${VAL}'/g' test.json --> but this is throwing the error
sed: -e  expression #1, char 16: unterminated `s' command

Please help.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Anything which contains whitespace needs to be quoted. You want `sed "s/%KEYVALUES%/${VAL}/" test.json` (probably without the `/g` but put it back if you genuinely expect multiple matches on the same line). – tripleee Jun 05 '21 at 18:20
  • 1
    @triplee this isn't a job for sed since sed doesn't understand literal strings and so that would fail given various values of `VAL`, e.g. `&` or `\1` or `/`. – Ed Morton Jun 05 '21 at 18:47
  • Still a common FAQ; probably edit to redirect to a different duplicate than reopen. – tripleee Jun 05 '21 at 19:11
  • Agreed but after searching the archives for as long as I'm willing to I don't know of a question to mark it as a duplicate of. – Ed Morton Jun 06 '21 at 11:47

1 Answers1

1
$ cat ENV.json
{"name": "VAULT", "value": "jsakjkaj"},{"name": "IMAGE", "value": "busybox"}

$ cat test.json
foo %KEYVALUES% bar

$ val=$(<ENV.json)
$ echo "$val"
{"name": "VAULT", "value": "jsakjkaj"},{"name": "IMAGE", "value": "busybox"}

$ val="$val" awk 'BEGIN{old="%KEYVALUES%"; new=ENVIRON["val"]} s=index($0,old){$0 = substr($0,1,s-1) new substr($0,s+length(old))} 1' test.json
foo {"name": "VAULT", "value": "jsakjkaj"},{"name": "IMAGE", "value": "busybox"} bar

If it were me, though, I wouldn't read ENV.json into a shell variable first, I'd just do it all in awk:

$ awk '
    BEGIN { old="%KEYVALUES%" }
    NR==FNR { new=(NR>1 ? new ORS : "") $0; next }
    s=index($0,old) { $0 = substr($0,1,s-1) new substr($0,s+length(old)) }
1' ENV.json test.json
foo {"name": "VAULT", "value": "jsakjkaj"},{"name": "IMAGE", "value": "busybox"} bar
Ed Morton
  • 188,023
  • 17
  • 78
  • 185