【发布时间】:2017-11-08 07:21:00
【问题描述】:
假设我有一个这样的句子
The quick, (brown, fox) jumps over, the (lazy, old, dog)
我想将括号 () 内的所有逗号 , 替换为冒号 :,但是不应该替换括号中的逗号。输出应该是这样的
The quick, (brown: fox) jumps over, the (lazy: old: dog)
请注意,每个句子中逗号和括号的数量不是固定的。 如何使用 shell 相关工具(bash、sed、awk 等)来达到这个效果?
我尝试使用sed 's/[^(]*\(([^)]*)\)/\1/g; s/,/:/g' 替换所有匹配的组,但不知道如何将结果插入到原始句子中。
【问题讨论】: