I'm parsing come legacy C-code using grep and sed and when trying to replace square brackets, something strange happened.
The following code to replace square opening brackets works fine:
$ echo "xyx[xzx]xyx" | sed 's|[\[]| |g'
results in:
xyx xzx]xyx
When I now add \]
to the string to sed, to also replace square closing brackets, it stops working:
$ echo "xyx[xzx]xyx" | sed 's|[\[\]]| |g'
now results in:
xyx[xzx]xyx
As far as I know, this is the proper way to escape square brackets.
What am I doing wrong?
I'm running this on a Ubuntu 14.04 machine.
You don't even need to escape:
echo "xyx[xzx]xyx" | sed 's|[][]| |g'
xyx xzx xyx
However keep in mind that order of ]
then [
is important here.
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