如何替换括号内的一个字符保持其他所有内容不变 [英] How to replace one character inside parentheses keep everything else as is

查看:24
本文介绍了如何替换括号内的一个字符保持其他所有内容不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

The data looks like this :

There is stuff here (word, word number phrases)
(word number anything, word phrases), even more
...

There is a lot of them in different files. There is different kind of data too, all around it that isn't in the same format. The data inside the paratheses can't change, and it's always on the same line. I do not have to deal with:

(stuff number,
maybe more here)

I would like to be able to replace the comma with a colon

Desired output would be

There is stuff here (word: word number phrases)
(word number anything: word phrases), even more
...

解决方案

Assuming there's only one comma to be replaced inside parentheses, this POSIX BRE sed expression will replace it with colon:

sed 's/((.*),(.*))/(1:2)/g' file

If there are more than one comma, only the last one will be replaced.

In multiple-commas scenario, you can replace only the first one with:

sed 's/(([^,]*),([^)]*))/(1:2)/g' file

这篇关于如何替换括号内的一个字符保持其他所有内容不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆