sed命令失败,带大括号和双引号 [英] sed command failure with curly braces and double quotes values
问题描述
我有一个包含以下内容的文件
(ABC)
/ pre>
我使用以下命令创建一个env变量
setenv ABC {a:{b:http:// c,d:http:// e}}
然后我运行sed命令
sed's |(ABC)| (''$ ABC'')|'myFile
这个返回这个
a:b:http:// ca:d:http:// e
它实际上返回这个
{a:{ b:http:// c,d:http:// e}
任何关于我失踪的想法
解决方案由于大括号和双引号是C shell中的元字符串,您必须使用反斜线来转义它们,如下所示:
setenv ABC \ {\a\:\ {\\ \\b \:\http:// c\,\d \:\http:// e\\} \}
或者更好的是,使用单引号
将完整的值包装起来setenv ABC'{a:{b:http:// c,d:http:// e}} '
无论哪种方式,你会得到:
$ $ $ $ $ $ $ $$$$$$$$$$ }}
与相反,如果您不逃避或引用在 setenv
语句中的值,就像您在问题中所做的那样,你会得到:
code> $ echo$ ABC
a:b:http:// ca:d:http:// e
这就是你所得到的( sed
与问题无关)。
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
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).
这篇关于sed命令失败,带大括号和双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!