2010-08-06 156 views
0
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} 

名字的價值有一個雙引號,我只是想更換雙引號單報價,而不是所有雙引號,請告訴我如何解決它?

+0

在Python或Perl(或獨立版本)中使用適當的JSON解析器可能會更好。 – 2010-08-06 14:57:41

+0

[需要幫助! sed或awk如何替換文本](http://stackoverflow.com/questions/3422103/need-help-sed-or-awk-how-to-replace-text) – 2010-08-06 15:00:18

回答

0
awk '{for(i=1;i<=NF;i++) if($i~/name/){ gsub("\042","\047",$(i+1)) }}1' file