linux - LINUX 中的替换模式

标签 linux bash shell

2.txt

"platform": "iPhone","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

"platform": "android","report_attrs": {"SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

我想将 ,*:*{ 替换为 ,

o/p:

"platform": "iPhone","SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

"platform": "android","SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

这是我尝试过的:

sed s/,*:*{/,/g 2.txt

最佳答案

sed 正则表达式中的

,* 表示匹配零次或多次出现的 , ,而 :* 表示匹配零次或多次出现的 : 符号。

通过 sed,

$ sed 's/,[^:]*:[^{]*{/,/' file

"platform": "iPhone","SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

"platform": "android","SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

或者

$ sed 's/,[^{]*{/,/' file

"platform": "iPhone","SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

"platform": "android","SKU": "XYZ123","Unit Price": "32.25","Qty": "3"}

关于linux - LINUX 中的替换模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25394761/

相关文章:

mysql - 我应该为我的 CentOS 服务器安装哪个 mysql 版本?

Linux:如何在文件中的特定行号处插入一行?

linux - 如何(分别)查找所有目录和子目录的文件总和,并按大小对它们进行排序

bash - 对目录中的所有文件执行命令

linux - 为什么bash无法正确读取我的文件

c - Linux线程id比较

bash - 在 ls bash 输出中添加 md5sum

windows - Unix 命令/脚本可以在 Windows 上运行吗?

linux - 从 shell 启动程序时是否可以分配窗口 ID?

Linux根据输入日期复制文件