V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
maosu
V2EX  ›  Linux

[请教] 关于文本替换多个相同字符串的方法

  •  
  •   maosu · 2022-11-04 11:36:56 +08:00 · 1376 次点击
    这是一个创建于 532 天前的主题,其中的信息可能已经有所发展或是发生改变。
    {name: "test1", server: xxxxxxx, port: 12345, type: ssr, cipher: chacha20-ietf, password: 12345, protocol: auth_aes128_sha1, obfs: plain, protocol-param: 15978:riUoXP, obfs-param: xxxxxxxxxxxxxxx, udp: true}
    {name: "test2", server: xxxxxxx, port: 12345, type: ssr, cipher: chacha20-ietf, password: 12345, protocol: auth_aes128_sha1, obfs: plain, protocol-param: 15999:riUoXP, obfs-param: xxxxxxxxxxxxxxx, udp: true}
    …………………………

    有多个这样的文本内容,我想把全文中 protocol-param 的参数两边加上单引号,请问用 sed,awk 之类的命令要如何写呢?

    我之前写了个:sed -r "s/protocol-param:(.*),/protocol-param:'\1',/g" Foo.yaml
    但是输出是:
    {name: "test1", server: xxxxxxx, port: 12345, type: ssr, cipher: chacha20-ietf, password: 12345, protocol: auth_aes128_sha1, obfs: plain, protocol-param:' 15978:riUoXP, obfs-param: xxxxxxxxxxxxxxx', udp: true}

    后面的'单引号加在了最后一个,逗号前了,V 友有什么好办法吗?
    2 条回复    2022-11-04 14:38:30 +08:00
    hxy100
        1
    hxy100  
       2022-11-04 12:22:58 +08:00   ❤️ 1
    sed -r "s/protocol-param:([^,]+),/protocol-param:'\1',/g" Foo.yaml
    maosu
        2
    maosu  
    OP
       2022-11-04 14:38:30 +08:00
    @hxy100 非常感谢,这个可行呐!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5302 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:53 · PVG 15:53 · LAX 00:53 · JFK 03:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.