使用awk替换双引号单引号
问题描述:
BEGIN {
q = "\""
FS = OFS = q ", " q
}
{
split($1, arr, ": " q)
for(i in arr) {
if(arr[i] == "name") {
gsub(q, "'", arr[i+1])
# print arr[1] ": " q arr[2], $2, $3
}
}
}
我有一个JSON文件,一些数据是这样的:使用awk替换双引号单引号
{"last_modified": {"type": "/type/datetime", "value": "2008-04-01T03:28:50.625462"}, "type": {"key": "/type/author"}, "name": "National Research Council. Committee on the Scientific and Technologic Base of Puerto Rico"s Economy.", "key": "/authors/OL2108538A", "revision": 1}
名字的价值有一个双引号,我只是想更换双引号单报价,而不是所有双引号,请告诉我如何解决它?
答
awk '{for(i=1;i<=NF;i++) if($i~/name/){ gsub("\042","\047",$(i+1)) }}1' file
在Python或Perl(或独立版本)中使用适当的JSON解析器可能会更好。 – 2010-08-06 14:57:41
[需要帮助! sed或awk如何替换文本](http://stackoverflow.com/questions/3422103/need-help-sed-or-awk-how-to-replace-text) – 2010-08-06 15:00:18