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" | sed -e "s/o+//g
expecting to see:
fbar
But instead I see
foobar
I can of course just expand the plus manually with:
echo "foobar" | sed -e "s/oo*//g"
but what do I have to do to get the plus sign working?
The special character in sed are the same as those in grep, with one key difference: the forward slash / is a special character in sed. The reason for this will become very clear when studying sed commands.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With