说我有这样的文本文件:
a;bc;d;{a;b;cd}
ab;cde;f;{ab;c;defg}
ab;{a;b;cd};cde;f
...
我希望用逗号代替大括号中的所有分号 。替换后它将如下所示:
a;bc;d;{a,b,cd}
ab;cde;f;{ab,c,defg}
ab;{a,b,cd};cde;f
...
我应该如何在shell命令中执行此操作? sed,awk或其他......
答案 0 :(得分:5)
Perl救援:
perl -pe 's/({.*?})/ $1 =~ s=;=,=gr /ge' input
问题是您的预期输出是错误的:
a;b;cd a;b;cd
| |
V V
a,b,c,d ab,cd
答案 1 :(得分:4)
通过perl使用positive lookahead,
$ perl -pe 's/;(?=[^{}]*})/,/g' file
a;bc;d;{a,b,cd}
ab;cde;f;{ab,c,defg}
ab;{a,b,cd};cde;f
答案 2 :(得分:0)
sed ':a; s/\(.*\)\({\)\([^;]*\)\(;\)\([^}]*}.*\)/\1\2\3,\5/;t a' file
在开头放置一个标签,然后关闭保持模式和循环,直到每行不再有成功的替换(t a)
E.g.
a;bc;d;{a;b;cd}morestuff{bsdj;dsfkjl;sdkjl;kd}
ab;cde;f;{ab;c;defg}
ab;{a;b;cd};cde;f
Output:
a;bc;d;{a,b,cd}morestuff{bsdj,dsfkjl,sdkjl,kd}
ab;cde;f;{ab,c,defg}
ab;{a,b,cd};cde;f