Quick Sed Question

Just want to know why when I do the following in sed, the required is not extracted.

echo "ab01cde234" | sed 's/[0-9]*$//'

result: ab01cde (Which is correct)

echo "ab01cde234" |sed 's/.*\([0-9]*\)$/\1/'

result: blank (was expecting 234)
or

echo "ab01cde234" |sed 's/.*\([0-9]\)*$/\1/'

result: blank (was expecting 234)

Am i missing something here? Also note that 234 can be has many digits, just need to extract the last section of the digits.

Thanks

you can try this;)

echo "ab01cde234" |sed 's/.\{7\}\([0-9]*\)$/\1/'
234

.* --> covers all string and sed does not consider your remaining exp that ( \([0-9]*\)$ )

regards
ygemici

Thanks ygemici, but the {7} is doing a fixed set of characters at the beginning of the string, is there any way just to extract the last set of digits, without fixing the initial character set.

Thanks
Ed

echo "ab01cde234" |sed 's/.*[^0-9]//'

The above matches the longst string which ends in something which is not a member of [0-9]. It substitutes the match with "nothing". Thus it returns the rest of the line.

More precise would be to force it to start from the start of the line.

this one?

echo "ab01cde234" | sed 's/.*[a-z]\([0-9]*\)$/\1/g'

--ahamed

Thks guys, the last two did the trick.

Many Thanks for your help on this.

Hi Ed
sed does not parse your expression for dynamic strings
if you has fixed string then it can be write with sed.
however you can use grep for your needs more sensiblely :wink:

echo "ab01cde234"|grep -o "[0-9]*$"
234

if you want to use sed , maybe you can try like this

echo "ab01cde234" | sed 's/.*[a-zA-Z]\([0-9][0-9]*\)[A-Za-z]*/\1/g'
234

or

echo "abca121a1b01cde23412zxxx1212aa" | sed 's/.*[a-zA-Z]\([0-9][0-9]*\)[A-Za-z]*/\1/g'
1212

this expression only make this examples not global!.
for example

echo "ab01cde234AA" | sed 's/.*[a-z]\([0-9]*\)$/\1/g'
ab01cde234AA

regards
ygemici