使用 awk 将双引号替换为单引号

发布于 2024-09-13 12:06:59 字数 637 浏览 5 评论 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文件,一些数据如下:

{"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}

名称的值有双引号,我只想将双引号替换为单引号,而不是全部双引号,请告诉我如何修复它?

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  
        }
    }
}

I have a json file, some data like this:

{"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}

The name's value have a double quote, I only want to replace the double quote to single quote , not the all double quote, please tell me how to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2024-09-20 12:06:59
awk '{for(i=1;i<=NF;i++) if($i~/name/){ gsub("\042","\047",$(i+1)) }}1' file
awk '{for(i=1;i<=NF;i++) if($i~/name/){ gsub("\042","\047",$(i+1)) }}1' file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文