Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed substitute recursively

Tags:

regex

sed

echo ddayaynightday | sed 's/day//g'

It ends up daynight

Is there anyway to make it substitute until no more match ?

like image 307
w00d Avatar asked Apr 02 '12 20:04

w00d


2 Answers

My preferred form, for this case:

echo ddayaynightday | sed -e ':loop' -e 's/day//g' -e 't loop'

This is the same as everyone else's, except that it uses multiple -e commands to make the three lines and uses the t construct—which means "branch if you did a successful substitution"—to iterate.

like image 97
torek Avatar answered Dec 03 '22 19:12

torek


This might work for you:

echo ddayaynightday | sed ':a;s/day//g;ta'
night
like image 20
potong Avatar answered Dec 03 '22 17:12

potong