sed command failure with curly braces and double quotes values

烂漫一生 提交于 2019-12-25 08:43:19

问题


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


回答1:


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).



来源:https://stackoverflow.com/questions/45329073/sed-command-failure-with-curly-braces-and-double-quotes-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!