V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
maosu
V2EX  ›  Linux

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

  •  
  •   maosu · Nov 4, 2022 · 2081 views
    This topic created in 1270 days ago, the information mentioned may be changed or developed.
    {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 replies    2022-11-04 14:38:30 +08:00
    hxy100
        1
    hxy100  
       Nov 4, 2022   ❤️ 1
    sed -r "s/protocol-param:([^,]+),/protocol-param:'\1',/g" Foo.yaml
    maosu
        2
    maosu  
    OP
       Nov 4, 2022
    @hxy100 非常感谢,这个可行呐!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1028 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:01 · PVG 03:01 · LAX 12:01 · JFK 15:01
    ♥ Do have faith in what you're doing.