tags:

views:

105

answers:

5

Now according to all the literature

echo 1234abcd|sed "s|[0-9]\+|#|g"

should return #abcd. And

echo abcd|sed "s|[0-9]\+|#|g"

should return abcd.

But on OS X 10.4.11 the first expression returns 1234abcd. Using * instead of + works for the first example but fails on the second, returning #abcd, because the [0-9] pattern is matched zero times.

Does the + operator not work in regular expressions in OS X? Is there an alternative?

Thanks

+2  A: 

Obsolete basic regular expressions do not support + and ? quantifiers. They are regular characters.

Alternatives for [0-9]+ are e.g. [0-9]{1,} or [0-9][0-9]*.

Or you can use sed -E to use modern, extended regular expressions.

laalto
+3  A: 

On OSX, sed by default uses basic REs. You should use sed -E if you want to use modern REs, including the "+" one-or-more operator.

See here for the indication that sed uses basic REs by default, here for the modern RE syntax, and here for the basic RE (ed) information.

paxdiablo
A: 

you can use awk

# echo 1234abcd| awk '{gsub(/[0-9]+/,"#")}1'
#abcd

# echo abcd| awk '{gsub(/[0-9]+/,"#")}1'
abcd
ghostdog74
I guess I'll have to learn awk next. I'm still getting my head around sed though.
stib
not going to stop you from learning sed, but once you know awk in and out, there is no need to use sed anymore.
ghostdog74
+1  A: 

Many of the OS X unix utilities are of versions that lack the comforts of their GNU equivalents. As Pax says, you can use -E:

drigz@mbp drigz 0$ echo 1234abcd | /usr/bin/sed "s/[0-9]\+/#/g" 
1234abcd
drigz@mbp drigz 0$ echo 1234abcd | /usr/bin/sed -E "s/[0-9]+/#/g" 
#abcd

Note that small changes to the syntax of your regex are required (\+ to + in this case).

However, I prefer to use fink to get GNU utilities:

drigz@mbp drigz 0$ echo 1234abcd | /sw/bin/sed "s/[0-9]\+/#/g"
#abcd
drigz@mbp drigz 0$ /sw/bin/sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
Rodrigo Queiro
I'm assuming that you meant \\+ to + (the formatting seems to have eaten the escape character)Unfortunately the firewall in this place seems to block fink, and macports, so there's a whole lot og GNU fun I can't access.
stib
hmm.. sometimes it eats the \ sometimes not
stib
Rodrigo Queiro
Good idea. I'll go look for the source.
stib
+2  A: 

If + doesn't work, you can always use {1,}

Marcin
Oh, I see, of course. Thanks.
stib