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

2022-11-04 11:36:56 +08:00
 maosu
{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 友有什么好办法吗?
1392 次点击
所在节点    Linux
2 条回复
hxy100
2022-11-04 12:22:58 +08:00
sed -r "s/protocol-param:([^,]+),/protocol-param:'\1',/g" Foo.yaml
maosu
2022-11-04 14:38:30 +08:00
@hxy100 非常感谢,这个可行呐!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/892595

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX