i'm trying to replace an exact string that includes bracket on it. let's say: a[aa] to bbb, just for giving an example.
I had used the following regex:
sed 's|\<a\[aa]\>|bbb|g' testfile
but it doesn't seem to work. this could be something really basic but I have not been able to make it work so I would appreciate any help on this.
You need to remove the trailing word boundary that requires a letter, digit or
_to immediately follow the]char.See the online
seddemo:You may also require a non-word char with a capturing group and replace with a backreference:
Here,
([^_[:alnum:]]|^)captures any non-word char or start of string into Group 1 and([^_[:alnum:]]|$)matches and caprures into Group 2 any char other than_, digit or letter, and the\1and\2placeholders restore these values in the result. This, however, does not allow consecutive matches, so you may still use\<beforeato play it safe:sed -E 's~\<a\[aa]([^_[:alnum:]]|$)~bbb\1~g'. file`.See this online demo.
To enforce whitespace boundaries you may use
Or, in your case, just a trailing whitespace boundary seems to be enough: