Professional Web Applications Themes

grep & regexp - SCO

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 ...

  1. #1

    Default grep & regexp

    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 Guest

  2. #2

    Default Re: grep & regexp

    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
    Jean-Pierre Guest

  3. #3

    Default Re: grep & regexp

    On Fri, Oct 03, 2003, Roberto Zini wrote: 

    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: 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
    Bill Guest

  4. #4

    Default Re: grep & regexp

    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 - 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).
    Bob Guest

  5. #5

    Default Re: grep & regexp

    In article <iglou.com>,
    Bob Stockler <iglou.com> wrote: 

    Or something like:

    a=123.45678
    tail=${a#*.??}
    [[ $tail != $a ]] && a=${a%$tail}

    John
    --
    John DuBois com KC6QKZ/AE http://www.armory.com/~spcecdt/
    John Guest

  6. #6

    Default Re: grep & regexp

    John DuBois wrote: 
    >
    >
    > Or something like:
    >
    > a=123.45678
    > tail=${a#*.??}
    > [[ $tail != $a ]] && a=${a%$tail}
    >
    > John[/ref]

    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)

    Roberto Guest

  7. #7

    Default Re: grep & regexp

    Bill Campbell wrote: 

    [snip]
     
     


    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)

    Roberto Guest

Similar Threads

  1. Learning to use map and grep
    By Paul Kraus in forum PERL Beginners
    Replies: 3
    Last Post: January 23rd, 03:49 PM
  2. grep with [ ] brackets ... ?
    By Luke Bakken in forum PERL Beginners
    Replies: 3
    Last Post: September 23rd, 02:14 PM
  3. grep with ftp
    By Shaunn Johnson in forum PERL Beginners
    Replies: 9
    Last Post: September 12th, 06:51 AM
  4. Help with grep
    By Kevin in forum Sun Solaris
    Replies: 3
    Last Post: September 4th, 07:11 AM
  5. grep
    By Anders Bystrup in forum PERL Miscellaneous
    Replies: 3
    Last Post: August 7th, 09:36 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139