Hi All; I've been trying to write a sed function to return only a numeric portion of a string, but can't seem to get it working. The input is a single string of letters and numbers, with the numbers always consecutive. For example: BUILD-AM005-a
I want to get the 005 out of this string.
echo BUILD-AM005-a | sed 's/.*([[:digit:]]).*/\1/g'
will return the digit 5. This is good!
So I add an asterisk to try to match multiple digits like: echo BUILD-AM005-a | sed 's/.*([[:digit:]]*).*/\1/g'
and instead of returning 005, it doesn't match anything, so returns nothing.
Can any of you RE maters help me out?
Steve Moffat IBM Global Services sjm@ca.ibm.com (204)792-3245
# echo BUILD-AM005-a | sed 's/[^0-9]//g' 005
Sean
On 5/9/07, Steve Moffat Steve.Moffat@ca.ibm.com wrote:
Hi All; I've been trying to write a sed function to return only a numeric portion of a string, but can't seem to get it working. The input is a single string of letters and numbers, with the numbers always consecutive. For example: BUILD-AM005-a
I want to get the 005 out of this string.
echo BUILD-AM005-a | sed 's/.*([[:digit:]]).*/\1/g'
will return the digit 5. This is good!
So I add an asterisk to try to match multiple digits like: echo BUILD-AM005-a | sed 's/.*([[:digit:]]*).*/\1/g'
and instead of returning 005, it doesn't match anything, so returns nothing.
Can any of you RE maters help me out?
Steve Moffat IBM Global Services sjm@ca.ibm.com (204)792-3245
Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable
I'm confused; what do you mean by "get the 005 or of this string" ?
You want the output to look like:
BUILD-AM-a
or
005
?
I don't think sed is what you want.
I believe this:
echo BUILD-AM005-a | sed 's/.*([[:digit:]]).*/\1/g'
Returns 5 because you are substituting everything before and after the digit with nothing.
Are the strings the same length all the time? Would:
echo BUILD-AM005-a | cut -c 9-11
do?
If not then maybe:
echo BUILD-AM005-a | grep -E -o [0-9]{1,}
John
On Wed, 2007-05-09 at 14:52 -0500, Steve Moffat wrote:
Hi All; I've been trying to write a sed function to return only a numeric portion of a string, but can't seem to get it working. The input is a single string of letters and numbers, with the numbers always consecutive. For example: BUILD-AM005-a
I want to get the 005 out of this string.
echo BUILD-AM005-a | sed 's/.*([[:digit:]]).*/\1/g'
will return the digit 5. This is good!
So I add an asterisk to try to match multiple digits like: echo BUILD-AM005-a | sed 's/.*([[:digit:]]*).*/\1/g'
and instead of returning 005, it doesn't match anything, so returns nothing.
Can any of you RE maters help me out?
Steve Moffat IBM Global Services sjm@ca.ibm.com (204)792-3245
Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable