Village
Village

Reputation: 24383

How to remove square brackets and any text inside?

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 .

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

Upvotes: 4

Views: 10355

Answers (2)

Amitanshu Gupta
Amitanshu Gupta

Reputation: 1

sed 's/([^])*)/replacement text/g' in case of Parenthesis ()

Upvotes: -1

Kent
Kent

Reputation: 195059

try this sed line:

sed 's/\[[^]]*\]//g' 

example:

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 .

explanation:

the regex is actually straightforward:

\[     #match [
[^]]*  #match any non "]" chars
\]     #match ]

so it is

match string, starting with [ then all chars but ] and ending with ]

Upvotes: 13

Related Questions