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

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

问题描述

数据看起来像这样:

 这里有东西(单词,单词数字短语)
(单词数字,单词短语),甚至更多
...

有很多他们在不同的文件。也有不同种类的数据,所有数据都不是相同的格式。内容中的数据不能改变,它总是在同一行上。我不必处理:

pre $ (填充数字,
可能更多)
code>

我希望能用冒号代替逗号



期望的输出是

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $短语),甚至更多
...


解决方案

假设在括号内只有一个逗号被替换,这个 POSIX BRE sed 表达式将用冒号取代它:

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

如果有多个逗号,只会替换最后一个



在多逗号的情况下,您可以将替换为第一个

  sed's /(\([^,] * \),\([^)] * \))/(\ 1:\ 2)/ g'文件


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天全站免登陆