Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 找到一个模式并替换其中的一个字符_Linux_Bash_Shell_Sed - Fatal编程技术网

Linux 找到一个模式并替换其中的一个字符

Linux 找到一个模式并替换其中的一个字符,linux,bash,shell,sed,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"} 输出: "pla

这在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"}
"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

下面的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

这是一个
awk

awk -F"[][]" 'NF>1 {gsub(/,/,"*",$2);$2="["$2"]";sub(/ \[/,"[")}1' 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"}

这是一个
awk

awk -F"[][]" 'NF>1 {gsub(/,/,"*",$2);$2="["$2"]";sub(/ \[/,"[")}1' 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"}
我的sed解决方案:

$ sed 's/[[]\(.\+\),\(.\+\)[]]/[\1|\2]/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"}
我的sed解决方案:

$ sed 's/[[]\(.\+\),\(.\+\)[]]/[\1|\2]/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:

perl -pE 's/\[(.*?),(.*?)\]/[\1|\2]/g' <<EOF
"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"}
EOF
下一个perl:

perl -pE 's/\[(.*?),(.*?)\]/[\1|\2]/g' <<EOF
"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"}
EOF
posix版本(使用gnu sed的so
--posix
) 考虑的不仅仅是同一行中[]和几个[]之间的简单夫妇。这个递归调用需要一个接一个地更改每个事件,直到没有更多的案例

posix版本(使用gnu sed的so
--posix

考虑的不仅仅是同一行中[]和几个[]之间的简单夫妇。这个递归调用需要一个接一个地更改每个事件,直到没有更多的案例。

这只是在上讨论的问题的另一个实例

这应该做到:

$ awk -F'[][]' -v OFS= 'gsub(/,/,"|",$2){$2="["$2"]"}1' 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"}

这只是在会议上讨论的问题的又一个例子

这应该做到:

$ awk -F'[][]' -v OFS= 'gsub(/,/,"|",$2){$2="["$2"]"}1' 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"}

数组总是在最后一个值中,还是可以有几个数组?中间可以有几个数组?中间可以有几个数组,还是可以有几个数组?中间可以有几个数组?如果输入包含
[“Main”,“Login”]
@AvinashRaj,当然;)当它不起作用时,可以构造许多变体。。例如,当最后一个
]
丢失时,等等:)或者当逗号丢失时…;)如果输入包含
[“Main”、“Login”]
@AvinashRaj,当然;),则它将不起作用当它不起作用时,可以构造许多变体。。例如,当最后一个
]
丢失时,等等:)或者当逗号丢失时…;)适用于样本请求,但限于[]中的1[]和max 2字段。因此,对于样本请求来说,这是一项精细的工作,但仅限于[]中的1[]和max 2字段。根据这一点,它不像perl中的反向引用,但是有一种if/goto可以通过递归调用实现这一点。塞德对“从火星来”的一口东西有一些逻辑上的看法没有像perl中那样的反向引用,但是有一种if/goto可以通过递归调用实现这一点。塞德对“从火星来”的一口东西有一些逻辑上的看法