Results 1 to 10 of 10

Thread: decimals in awk

  1. #1
    Join Date
    Apr 2004
    Posts
    8

    Unanswered: decimals in awk

    Hi All, I'm new to dBforums and new to UNIX. I am taking a UNIX class and have a homework assignment that seems to require lots of "exploring" outside the scope of the textbook.
    I have a file that I have to calculate a sum and display the contents of the file along with the calculated sum like this: $xx.xx. I'm using awk and here's what I have so far:
    awk 'BEGIN { FS = ":"; print "Name \t\t From \t\t To\t\t Dur\t Charge\t" }
    {
    {if ($6 == 1)
    {
    Charge=$5 * .05 + '2.00'
    }
    else
    {
    Charge=$5 * .10 + 3.00
    }
    }
    print $1, $2"\t", $3"\t", $4"\t", $5"\t", "$"Charge
    }' tn-data

    my problem is that I can't seem to find a way to get a two digit decimal value to print if the value is zero. Ex: $5.00 displays as $5

    Can someone please help?

    Thanks,
    Chris

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    awk 'BEGIN { FS = ":"; print "Name \t\t From \t\t To\t\t Dur\t Charge\t" }
    {
    printf(%s %s\t%s\t%s\t%s\t"$"%.2f\n", $1, $2, $3, $4, $5, ($6 == 1) ? ($5 * .05 + 2.00) : ($5 * .10 + 3.00) );
    }' tn-data

  3. #3
    Join Date
    Apr 2004
    Posts
    8
    Wow! That is so much easier then what I did. I didn't realize you could essentially write an if statement within printf ... and without if then else. I'll give it a try. Thanks so much for your help.

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    ooops, sorry - mis-type:

    awk 'BEGIN { FS = ":"; print "Name \t\t From \t\t To\t\t Dur\t Charge\t" }
    {
    printf("%s %s\t%s\t%s\t%s\t $%.2f\n", $1, $2, $3, $4, $5, ($6 == 1) ? ($5 * .05 + 2.00) : ($5 * .10 + 3.00) );
    }' tn-data

  5. #5
    Join Date
    Apr 2004
    Posts
    8
    Thanks. I tried it the first time and noticed the double quote missing when I received an error. The error that I'm getting is from line one though. I tried the new one that you posted and still get the same error. Here's the error I'm getting, but I don't see a syntax error. It's exactly the same as I had it initially.

    awk: syntax error near line 3
    awk: illegal statement near line 3

    ?

  6. #6
    Join Date
    Apr 2004
    Posts
    8
    sorry, the error is actually referring to the first line of code. In my script that's were the code starts.

    Chris

  7. #7
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    pls post your script and a smple data file.

  8. #8
    Join Date
    Apr 2004
    Posts
    8
    I copied what you had written into a new script:
    #!/usr/local/bin/bash

    awk 'BEGIN { FS = ":"; print "Name \t\t From \t\t To\t\t Dur\t Charge\t" }
    {
    printf("%s %s\t%s\t%s\t%s\t $%.2f\n", $1, $2, $3, $4, $5, ($6 == 1) ? ($5 * .05
    + 2.00) : ($5 * .10 + 3.00) );
    }' tn-data

    This is the result when I run it:
    bash-2.04$ awkAddress1
    awk: syntax error near line 3
    awk: illegal statement near line 3
    bash-2.04$

  9. #9
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    works with my awk under MKS.

    If you're under Solaris, try using 'nawk' instead of 'awk'

  10. #10
    Join Date
    Apr 2004
    Posts
    8
    Yes, it is Solaris. I will try nawk.

    Thanks for all your help.

    Chris

Posting Permissions

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