Home > Blockchain >  sed is failing when regex pattern is a group
sed is failing when regex pattern is a group

Time:09-16

My sed command works when I try to replace a single pattern.

sed -i '' 's/^\(feature\)/replacement/g' change.md

It doesn’t do anything when the regex is a group.

sed -i '' 's/^\(feature\|bug\|security\)/replacement/g' change.md

Does anyone know what I'm doing wrong? I tried a number of variations, but none seem to work.

My file:

## Features
feature:foo
feature: baz
## Bugs
bug: bar
bug: KK

CodePudding user response:

On sed refers to re_format(7) man page to describe regular expressions. Without -E, sed uses "basic regular expressions". The re_format man page calls these "obsolete REs".

This sentence seems to answer the question:

Obsolete (“basic”) regular expressions differ in several respects. ‘|’ is an ordinary character and there is no equivalent for its functionality.

(emphasis mine)

It appears sed -E 's/^(feature|bug|security)/replacement/g' is your easiest option.

Or perl -i -pe 's/^(feature|bug|security)/replacement/g' file

  • Related