With sed, I can replace one item with another. E.g.:
sed -i "s/a/b/g" file
However, there are times when I do not want this replacement to happen. How can I ensure that the replacement only happens when:
- The character immediately before is not a letter, number, or
{. - The character immediately after is not a letters or number, or
}.
([^0-9a-zA-Z\{])– is a first matched group of symbols (length of 1 in current case). Not a number (0-9), not a letter (a-zA-Z) and not a { (\{).([^0-9a-zA-Z\}])– is a second matched group, just like first.If we matched those sequences, we substitute it with first group (
\1),band second group (\2).