这在 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/