4

I have a document containing some text inside square brackets, e.g.:

The fish [ate] the bird.
[This is some] text.
Here is a number [1001] and another [1201].

I need to delete all of the information contained inside the square brack and the brakets, e.g.:

The fish  the bird.
 text.
Here is a number  and another .
  • I tried sed -r 's/\[[+]\]//g' file.txt, but this did not work.

How can I delete anything in the pattern [<anything>]?

4

2 回答 2

13

试试这条 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 ]

所以它是

匹配字符串,从[所有字符开始,但]]

于 2013-07-08T09:14:17.303 回答
-1

sed 's/([^])*)/替换文本/g' 在括号 () 的情况下

于 2016-08-11T04:53:05.870 回答