我有一个文档,其中包含方括号内的一些文本,例如:
The fish [ate] the bird.
[This is some] text.
Here is a number [1001] and another [1201].
我需要删除方括号和方括号内的所有信息,例如:
The fish the bird.
text.
Here is a number and another .
sed -r 's/\[[+]\]//g' file.txt
,但这不起作用。如何删除模式[<anything>]
中的任何内容?
答案 0 :(得分:9)
尝试这个sed行:
sed 's/\[[^]]*\]//g'
示例:
kent$ echo "The fish [ate] the bird.
[This is some] text.
Here is a number [1001] and another [1201]."|sed 's/\[[^]]*\]//g'
The fish the bird.
text.
Here is a number and another .
说明:
正则表达式实际上很简单:
\[ #match [
[^]]* #match any non "]" chars
\] #match ]
所以它是
匹配字符串,从[
开始,然后是]
但以]
结尾的所有字符
答案 1 :(得分:-1)
sed's /([^])*)/ 替换文本 / g'