Skip to content
Advertisement

Sed replace all matches after some patterns

I want to replace all B after ‘=’.

echo A, B = A B B A B B B | sed 's/=(.*)B(.*)/=1C2/g'

The expected result should be

A, B = A C C A C C C

But I got this result:

A, B = A B B A B B C

Only the last matched pattern be replaced. How to resolve it?

Advertisement

Answer

Use this sed:

sed ':loop; s/(=.*)B(.*)/1C2/; t loop'

Test:

$ echo A, B = A B B A B B B | sed ':loop; s/(=.*)B(.*)/1C2/; t loop'
A, B = A C C A C C C
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement