linux - 找到一个模式并替换它的一个字符

标签 linux bash shell sed

这在 sed 中可行吗?

我想将以 [ 开始并以 ] 结束的元组中的 , 替换为 |

"platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main","Login"]
"platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

输出:

"platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main"|"Login"]
"platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

如果不可能,我们将不胜感激任何其他解决方案。

谢谢。

最佳答案

下面的 perl 代码将用 | 符号替换 [] 大括号内的所有 ,

$ perl -pe 's/,(?=[^\[\]]*\])/|/g' file
"platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"},"screen_flow":["Main"|"Login"]
"platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

要保存所做的更改,

perl -i -pe 's/,(?=[^\[\]]*\])/|/g' file

关于linux - 找到一个模式并替换它的一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25403660/

相关文章:

linux - 指定直接主机的 Ansible ad-hoc 命令 - 没有匹配的主机

linux - 使 Eclipse 控制台按钮永久化?

regex - 为什么这个简单的 bash 正则表达式不返回 true?

bash - 在 bash shell 中追加运算符

linux - Linux 上的 netcat 代理 shell 问题

python-2.7 - 如何在unix中的多个文件中添加一行

linux - 放置自定义可执行文件或脚本的 Unix 标准目录?

c - 使用 iptables TPROXY 重定向到原始套接字

linux - 从 Linux 命令行中,查找字符串出现的行数

linux - 尝试在 bash linux 中使用 "new line"命令复制语法