On Fri, Oct 03, 2003, Roberto Zini wrote:
>Hi.
>
>I know Friday's not a great day to start dealing with regular
>expressions but this is what I have to do today :-)
>
>Supposing I have the following ksh variable:
>
>myvar="123.4567"
>
>What grep syntax do I have to use as to have only the first 2 decimal
>digits printed on output ?
>
>This is what I'd like to achieve:
>
> 123.45
>
>I've tried several grep variants but unsuccessully.
>
>I've done the trick by using printf but I'd like to achieve it using
>grep (eg, echo $myvar | grep ".*\.[:digit:]{2}" ), just to test its
>regexp capabilities.
Grep only selects lines from files, it doesn't do any editing of those
files. For that you want to use another tool, and it depends on whether
you want to just truncate the extra digits or do proper rounding.
For numeric manipulations you may want to use the ``bc'' program, setting
the scale appropriately, or perhaps perl (the double quotes below around
the perl -e argument expand $variable from your shell, and the qq() is
perlish for double quotes that makes it easier to do things like this from
the shell).
value=`perl -e "printf (qq(%f.2d), $variable);"`
Bill
--
INTERNET:
bill@Celestial.COM Bill Campbell; Celestial Systems, Inc.
UUCP: camco!bill PO Box 820; 6641 E. Mercer Way
FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676
URL:
http://www.celestial.com/
``The meek shall inherit the Earth, the rest of us will go to the stars...''
-Dr. Isaac Asimov