如何在bash中将单引号替换为双引号?

时间:2017-02-02 11:33:09

标签: bash ubuntu sed

我有一个以下字符串:

 {"name":"ram","details":[{'subjects':{'service_location':'c:/A.exe','url':'http://A.zip'}}]}

在上面的字符串中,几个字符串周围都有单引号。那么,如何在上面的字符串中用双引号替换单引号并得到如下:

 {"name":"ram","details":[{"subjects":{"service_location":"c:/A.exe","url":"http://A.zip"}}]}

4 个答案:

答案 0 :(得分:3)

@Learner:尝试sed解决方案:

sed "s/'/\"/g"  Input_file

OR

your_command | sed "s/'/\"/g" 

答案 1 :(得分:1)

扩展@ RavinderSingh13答案,使用以下命令转换你的字符串。

echo '{"name":"ram","details":'"[{'subjects':{'service_location':'c:/A.exe','url':'http://A.zip'}}]}" | sed "s/'/\"/g"

答案 2 :(得分:1)

使用tr翻译单个字符:

your_command | tr "'" '"'

或者,如果输入太多:

your_command | tr \' \"

: - )

答案 3 :(得分:0)

以下将所有单引号转换为双引号。这使用了awk内置的gsub函数来搜索和替换文本。在这里,我们告诉awk替换等于单引号的s。一旦找到单引号,请用双引号替换它。 1是打印线。

awk -v s="'" '{gsub(s,"\"")}1' file

OR

source-of-json | awk -v s="'" '{gsub(s,"\"")}1'