0

I set the env variable like this

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

I use sed like this

sed 's|(ENV)|('"$ENV"')|' aFile

It replaces (ENV) with the following

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

However if I set the variable like this within single quotes

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

It replaces like this

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

But I would like the output of sed for (ENV) to be

('{"a":{"b":"http://c","d":"http://e"}}')
randomir
  • 17,989
  • 1
  • 40
  • 55
user_mda
  • 18,148
  • 27
  • 82
  • 145

1 Answers1

1

First of all, it's important to quote (or escape) your variable on definition (with setenv), as explained in this answer, since braces and double quotes (among others) are metacharacters in C shell.

You can quote the complete value with single quotes:

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

Or, escape metacharacters with backslashes, like this:

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

Now, for the sed part of your question -- and how to quote the variable on replacement.

Just add single quotes in your sed replacement expression and you're done:

$ setenv ABC '{"a":{"b":"http://c","d":"http://e"}}'
$ echo '(ABC)' | sed 's|(ABC)|('"'$ABC'"')|'
('{"a":{"b":"http://c","d":"http://e"}}')

Note, in addition, you should also take care of escaping sed replacement metacharacters (these three only: \, /, and &) in your variable. You seem aware of this, since you already use | as a delimiter in you regex, but if you are unsure of what your variable contains, it's always good to escape it. For details on reliable escaping metacharacters in sed, see this answer. In short, to escape a single-line replacement pattern, run it through sed 's/[&/\]/\\&/g'.

randomir
  • 17,989
  • 1
  • 40
  • 55
  • Thanks but after the evaluation the value is returned only as a string between the single quotes. I want the exact same value to be evaluated. Along with the single quotes. – user_mda Jul 26 '17 at 16:04
  • Thank you but I should have been clearer, I cannot change the way I se the variable, but Can I use sed to add back the quotes while evaluatiing? – user_mda Jul 26 '17 at 16:13
  • .. and **please read** [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). It seems you're not a new user on SO, and yet seem to accept only your own answers, and upvote none of the others. That's not a nice way to behave on SO. – randomir Jul 26 '17 at 16:21
  • sure thanks, I generally try out the answer and accept if it works. Its also not nice to assume that I verified your answer and it worked for me – user_mda Jul 26 '17 at 17:12
  • Hey, seems to work thanks. For the answer and the downvote lol – user_mda Jul 28 '17 at 15:57
  • Ok, great. I've clarified your question a little bit and removed the dv. I've also updated my answer to better answer you question after all additional info from comments. I feel I answered the first part of your question in the answer to your previous question, though.. Btw, upvoting and accepting answers is a small thing you can do that goes a long way to help and thank the community for helping you. I haven't meant anything bad in my previous comment, but please make it a habit to upvote and accept. Thanks. – randomir Jul 28 '17 at 20:00