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 .
sed -r 's/\[[+]\]//g' file.txt
, but this did not work.How can I delete anything in the pattern [<anything>]
?
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 ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With