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

前端 未结 4 1095
佛祖请我去吃肉
佛祖请我去吃肉 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:54
    echo "foobar" | sed -e "s/o\\+//g"
    

    worked for me on Mac OS X 10.6.

    I remembered that I replaced my BSD version of sed with GNU sed 4.2, so this may or may not work for you.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-06 01:05

    You can use this, on linux or unix.

    echo "foobar" | perl -pe "s/o+//g"
    
    0 讨论(0)
  • 2020-12-06 01:06

    Sed is sad for regexes. You could either try the -E, which might work with BSD, or you could try this one instead:

    sed -e "s/o\{1,\}/"
    

    Perhaps there are too many sed's out there to have a usable tool on any system.

    0 讨论(0)
提交回复
热议问题