sed regexp

Hi,
I am not that good with reg exp and sed. But I was just looking at something the other day and came across a situation.
When I ran the below command:

echo "123 word" | sed 's/[0-9]*/(&)/'

the op was:

(123) word

But when I ran:

echo "123 word" | sed 's/[0-9]*/(&)/g'

the o/p was:

(123) ()w()o()r()d()

And when I ran

echo "123 word" | sed 's/[0-9]+/(&)/g'
or
echo "123 word" | sed 's/[0-9]+/(&)/'

nothing happened !!!
Can someone please help me out with an explanation?
Thanks in advance :slight_smile:

$ echo "123 word" | sed 's/[0-9]\+/(&)/g'
(123) word

I escaped the + so this could be the output you expected.

Sorry but nothing is happening after I tried ur command ... am I going wrong somewhere?

Maybe your sed is another version. What I posted works on gsed on my Linux box. So try this, which should work on older sed's too:

echo "123 word" | sed 's/[0-9]\{1,\}/(&)/g'
(123) word

the + or \+ says "1 or more". Writing it the long way means {1,} or \{1,\} which stands for the same.

sed uses a regex engine. Henry Spencer wrote one of the first complete ones. Since then implementations of sed have changed the regex over time. gsed is an example. This makes giving you an answer hard unless we know:

what OS (uname -a shows what we need)