V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fazero
V2EX  ›  正则表达式

sed 替换两个符号之间的内容为别的内容怎么写

  •  
  •   fazero · 2016-02-19 23:50:17 +08:00 · 1847 次点击
    这是一个创建于 2987 天前的主题,其中的信息可能已经有所发展或是发生改变。

    {
    "port_password":{
    "2016":"helloss",
    "1027":"wwwcc"
    },
    "method":"aes-256-cfb",
    "timeout":600
    }
    要把第三行中的 helloss 替换成 haha 可以下面这样
    sed -i 's/helloss/haha/g' filename
    但是 helloss 是不确定的,我只是想把第三行第二对双引号之间的内容替换成 haha ,应该怎么写啊

    4 条回复    2016-02-20 02:25:23 +08:00
    rrfeng
        1
    rrfeng  
       2016-02-20 00:00:04 +08:00
    人生苦短,请用 json 解析库……
    Strikeactor
        2
    Strikeactor  
       2016-02-20 00:02:17 +08:00   ❤️ 1
    sed 's/"2016":".*"/"2016":"haha"/'
    fazero
        3
    fazero  
    OP
       2016-02-20 00:10:43 +08:00
    @Strikeactor 谢谢,太强了
    SoloCompany
        4
    SoloCompany  
       2016-02-20 02:25:23 +08:00
    echo ‘
    {
    "port_password":{
    "2016":"hello\"ss",
    "1027":"wwwcc"
    },
    "method":"aes-256-cfb",
    "timeout":600
    }' | node -e '
    x=JSON.parse(require("fs").readFileSync("/dev/stdin"))
    x.port_password["2016"] = "f*ckgfw?"
    console.log(JSON.stringify(x))

    {"port_password":{"1027":"wwwcc","2016":"f*ckgfw?"},"method":"aes-256-cfb","timeout":600}
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   953 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:26 · PVG 05:26 · LAX 14:26 · JFK 17:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.