Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2003
    Posts
    14

    Arrow Unanswered: decimal point ....

    expr 5 / 2

    the out is 2, how do i get the output to display the decimal point ?

    i tried awk with printf but can't seem to get the correct command do help plz

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    'expr' can only perform integer arithmetic. There are probably more suitable utilities but seen as I tend to use awk for everything...

    echo 5 2 | awk '{print $1/$2}'

    HTH

  3. #3
    Join Date
    Oct 2003
    Posts
    14
    thanx guru, but what if i want to divide using variable ?

    eg i=5
    y=2

    how do i use $i/$y ? i tried awk '{print $i/$y}' doesn't seem to work .. can give further guidance guru ?

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Like this?

    echo $i $y | awk '{print $1/$2}'

  5. #5
    Join Date
    Oct 2003
    Posts
    14
    must i pass the variable into echo ? ... is there another way ? do you know what does awk 'printf' does ? i remember someone told me this but dont understand it

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    No, you needn't pass your variable in using echo. The thing is, awk must take an input from somewhere, so why not use echo? Awk doesn't have to work with, or output what you input but it does need something (otherwise it will expect you to enter input at the command line). You can get round this by doing your processing in a BEGIN block which includes an exit...

    awk -v i=5 -v y=2 'BEGIN {print i/y; exit}'

    The -v flag here is declaring variables to be used in the awk processing.

    HTH

  7. #7
    Join Date
    Oct 2003
    Posts
    14
    thanx guru

    i found another way using bc

    eg echo "scale=2; 5/2" | bc -l output 2.50 cause i put the scale 2
    Last edited by jinroh; 10-13-03 at 09:47.

  8. #8
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hello jinroh,
    what does the "eg" means in your posting ???

    For further operations whith the result from awk in your script try this
    i=5; y=2
    echo $i
    echo $y
    x=`echo $i $y | awk '{
    print $1/$2
    }'`
    echo $x

    Output to screen is "2,50"

    @Damian, thanks for your solutions. Iīm very avid.

    Greetings from Germany
    Peter F.
    Last edited by fla5do; 10-13-03 at 16:00.

Posting Permissions

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