logo       

RE: How to extract Numbers: msg#00068

editors.sed.user

Subject: RE: How to extract Numbers

Earlier, Kiran Gullapalli wrote:

> I've a string like this
>
> AAAxx.yy
> where AAA are letters and xx and yy are numbers. I
> want to extract xx.yy from the string. I'm doing
> some thing like this.
>
> echo abc12.34 | sed -e 's|^\D*\(\d.*\)|\1|g'
>
> but the result i'm getting is abc12.34 only. Where
> am i doing wrong ?

I don't think you can define letters as "\D" and numbers as "\d" with
sed. Try the last two suggestions in the list below if the lengths of
the letters/numbers fields may vary.
You also have a "g" switch on your command, but you have anchored the
"\D" to the start of the line so there would only ever be one match
per line at most, so the g is unnecessary.

Depending on what context your data is in, these seem to work for me
on bash shell:

# cut from the 4th char onwards
echo abc12.34 | cut -c4-

# remove the first 3 chars
echo abc12.34 | sed s/^...//

# select any numbers and dots that come after
# any number of letters
echo abc12.34 | sed 's/[A-Za-z]*\([0-9.]*\)/\1/'

# select any numbers and dots at the end of a line that
# come after any number of letters which end a line
echo abc12.34 | sed 's/^[A-Za-z]*\([0-9.]*\)$/\1/'

Hope that helps,
Pete



------------------------ Yahoo! Groups Sponsor --------------------~-->
Most low income homes are not online. Make a difference this holiday season!
http://us.click.yahoo.com/5UeCyC/BWHMAA/TtwFAA/dkFolB/TM
--------------------------------------------------------------------~->

--

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/sed-users/

<*> To unsubscribe from this group, send an email to:
sed-users-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/






<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise