问题
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