使用sed删除括号之间的字符串 [英] Using sed to delete a string between parentheses

查看:855
本文介绍了使用sed删除括号之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚如何仅在括号内删除括号之间的字符串.例如,我想删除字符串(Laughter)",但仅当它在括号中时才删除,而不仅仅是使它区分大小写,因为该字符串中的句子以Laughter开头.

I'm having trouble figuring how to delete a string in between parentheses only when it is in parentheses. For example, I want to delete the string "(Laughter)", but only when it's in parenthesis and not just make it a case sensitive deletion since a sentence within that string starts with Laughter.

推荐答案

我不确定我是否理解正确,但是以下内容将删除括号之间的文本:

I'm not sure I understand you correctly, but the following will remove text between parentheses:

sed "s/[(][^)]*[)]/()/g" myfile

(或者正如Llama在评论中指出的那样:)

(or as Llama pointed out in comments, just:)

sed "s/([^)]*)/()/g" myfile

它与字面量打开括号[(]相匹配,后跟任意数量的非)字符[^)]*,后跟字面量闭合括号[)].

It matches a literal open paren [(] followed by any number of non-) characters [^)]* followed by a literal close paren [)].

示例:

$ echo "Blah blah (potato) moo (cow is a pretty bird)(hello)" | sed "s/[(][^)]*[)]/()/g"
Blah blah () moo ()()

如果不想在输出中使用空括号,请在其中使用//而不是/()/.

Use // instead of /()/ there if you don't want the empty parens in the output.

这篇关于使用sed删除括号之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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