0

I have a file with the following content

(ABC)

I create an env variable with the following command

setenv ABC  {"a":{"b":"http://c","d":"http://e"}}

Then I run the sed command

sed 's|(ABC)|('"$ABC"')|' myFile

This returns with this

a:b:http://c a:d:http://e

It shuld actually return this

{"a":{"b":"http://c","d":"http://e"}

Any ideas on what I am missing

user_mda
  • 18,148
  • 27
  • 82
  • 145
  • probably shell issue... works for me on bash... `s="{'key':{'key':'value'}}"` followed by `echo '(ABC)' | sed 's/(ABC)/('"$s"')/'` gives `({'key':{'key':'value'}})` – Sundeep Jul 26 '17 at 14:02
  • Thanks Sandeep, I have actually updated the question with the actual values, the '/' in the http were an issue so I used pipe but now running into other issue, can you please look at the updated ques? – user_mda Jul 26 '17 at 14:54
  • again, it works for me on bash... my guess is still whatever shell you are using might need something else to work – Sundeep Jul 26 '17 at 15:13
  • 1
    so if I add single quotes while setting the variable it works for me – user_mda Jul 26 '17 at 15:20
  • hw to create an env variable with single qutes – user_mda Jul 26 '17 at 15:21
  • You need to specify what shell you are using. Add a tag please – Mad Physicist Jul 26 '17 at 15:30

1 Answers1

0

Since braces and double quotes are metacharacters in C shell, you have to either escape them with backslash, like this:

setenv ABC \{\"a\":\{\"b\":\"http://c\",\"d\":\"http://e\"\}\}

or, better, wrap the complete value in single quotes:

setenv ABC '{"a":{"b":"http://c","d":"http://e"}}'

Either way, you'll get:

$ echo "$ABC"
{"a":{"b":"http://c","d":"http://e"}}

On contrary, if you don't escape or quote the value in setenv statement, like you did in the question, you'll get:

$ echo "$ABC"
a:b:http://c a:d:http://e

and that's what you were getting (sed had nothing to do with the problem).

randomir
  • 17,989
  • 1
  • 40
  • 55