vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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. Thanks, Roberto -- Roberto Zini - Technical Support Manager - email:r.zini<AT>strhold.it Technical Support Manager -- Strhold Evolution Division R.E. (ITALY) --------------------------------------------------------------------- "Has anybody around here seen an aircraft carrier?" (Pete "Maverick" Mitchell - Top Gun) |
| |||
| Roberto Zini typed (on Fri, Oct 03, 2003 at 05:36:14PM +0200): | 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 is not what you want. Given some input, if grep matches any part of it, it prints all of the input line -- it doesn't do substitutions. You need a filtering program, like awk or expr or sed: echo $myvar | sed 's;\(.*\...\).*;\1;' After typing that, I simplified it to: echo $myvar | sed 's;\(\...\).*;\1;' -- JP |
| |||
| 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 |
| |||
| On Fri, Oct 03, 2003 at 05:36:14PM +0200, Roberto Zini wrote: | | Supposing I have the following ksh variable: | | myvar="123.4567" Since you said "ksh", use its parameter substitution: myvar="123.4567" myvar=${myvar%[0-9][0-9]} If the value might be: 123.45 123.456 123.4567 123.45678 ...... use a case-esac construction to supply the proper parameter substitution for the input variable. Bob -- Bob Stockler - bob@trebor.iglou.com Author: MENU EDIT II - The BEST Creator/Editor/Manager for filePro User Menus. Fully functional (time-limited) demos available by email request (specify OS). |
| |||
| In article <20031003131943.A13414@trebor.iglou.com>, Bob Stockler <bob@trebor.iglou.com> wrote: >Since you said "ksh", use its parameter substitution: > > myvar="123.4567" > myvar=${myvar%[0-9][0-9]} > >If the value might be: > > 123.45 > 123.456 > 123.4567 > 123.45678 > ...... > >use a case-esac construction to supply the proper >parameter substitution for the input variable. Or something like: a=123.45678 tail=${a#*.??} [[ $tail != $a ]] && a=${a%$tail} John -- John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/ |
| |||
| John DuBois wrote: > In article <20031003131943.A13414@trebor.iglou.com>, > Bob Stockler <bob@trebor.iglou.com> wrote: > >>Since you said "ksh", use its parameter substitution: >> >> myvar="123.4567" >> myvar=${myvar%[0-9][0-9]} >> >>If the value might be: >> >> 123.45 >> 123.456 >> 123.4567 >> 123.45678 >> ...... >> >>use a case-esac construction to supply the proper >>parameter substitution for the input variable. > > > Or something like: > > a=123.45678 > tail=${a#*.??} > [[ $tail != $a ]] && a=${a%$tail} > > John Guys, I have to apologize for this stupid post I made :-( Last Friday, while I was driving home, I realized that, after using grep for about 15 years, I completely missed its usage in this context (yup, getting older :-) I'd like to thank you very much for all the tips you provided me with; boy, from this group, I learn something new every day. Thanks, Roberto -- Best, Roberto -- Roberto Zini - Technical Support Manager - email:r.zini<AT>strhold.it Technical Support Manager -- Strhold Evolution Division R.E. (ITALY) --------------------------------------------------------------------- "Has anybody around here seen an aircraft carrier?" (Pete "Maverick" Mitchell - Top Gun) |
| ||||
| Bill Campbell wrote: > On Fri, Oct 03, 2003, Roberto Zini wrote: > [snip] > > 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, thanks for your help. Here's the correct syntax for the above command: value=`perl -e "printf (qq(%.2f), $variable);"` Please notice I removed the "f" after the "%" char and the trailing "d" after the "2". Thanks, Roberto -- Roberto Zini - Technical Support Manager - email:r.zini<AT>strhold.it Technical Support Manager -- Strhold Evolution Division R.E. (ITALY) --------------------------------------------------------------------- "Has anybody around here seen an aircraft carrier?" (Pete "Maverick" Mitchell - Top Gun) |