Sed substitute recursively

前端 未结 6 841
我在风中等你
我在风中等你 2020-12-28 18:35

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

It ends up daynight

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

6条回答
  •  隐瞒了意图╮
    2020-12-28 19:36

    The g flag deliberately doesn't re-match against the substituted portion of the string. What you'll need to do is a bit different. Try this:

    echo ddayaynightday | sed $':begin\n/day/{ s///; bbegin\n}'
    

    Due to BSD Sed's quirkiness the embedded newlines are required. If you're using GNU Sed you may be able to get away with

    sed ':begin;/day/{ s///; bbegin }'
    

提交回复
热议问题