How to escape plus sign on mac os x (BSD) sed?

前端 未结 4 1100
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 00:42

I\'m trying to find and replace one or more occurrences of a character using sed on a mac, sed from the BSD General Commands.

I try:

echo \"foobar\"          


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-06 00:58

    Using the /g flag, s/o//g is enough to replace all o occurrences.

    Why + doesn't work as expected: in old, obsolete re + is an ordinary character (as well as |, ?). You should specify -E flag to sed to make it using modern regular expressions:

    echo "foobar" | sed -E -e "s/o+//"
    # fbar
    

    Source: man 7 re_format.

提交回复
热议问题