如何删除方括号和里面的任何文字?

Vil*_*age 2 bash sed

我有一个文档,在方括号内包含一些文本,例如:

The fish [ate] the bird.
[This is some] text.
Here is a number [1001] and another [1201].
Run Code Online (Sandbox Code Playgroud)

我需要删除方括号和方括号内的所有信息,例如:

The fish  the bird.
 text.
Here is a number  and another .
Run Code Online (Sandbox Code Playgroud)
  • 我试过了sed -r 's/\[[+]\]//g' file.txt,但这没用.

如何删除模式中的任何内容[<anything>]

Ken*_*ent 9

尝试这个sed行:

sed 's/\[[^]]*\]//g' 
Run Code Online (Sandbox Code Playgroud)

例:

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 .
Run Code Online (Sandbox Code Playgroud)

说明:

正则表达式实际上很简单:

\[     #match [
[^]]*  #match any non "]" chars
\]     #match ]
Run Code Online (Sandbox Code Playgroud)

所以它是

匹配字符串,以[所有字符开头但]以#结尾]