| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

05-01-04, 00:02
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 8
|
|
|
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
|
|

05-01-04, 11:29
|
|
Registered User
|
|
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
|
|

05-01-04, 12:25
|
|
Registered User
|
|
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. 
|
|

05-01-04, 16:48
|
|
Registered User
|
|
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
|
|

05-01-04, 17:09
|
|
Registered User
|
|
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
?
|
|

05-01-04, 17:15
|
|
Registered User
|
|
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
|
|

05-01-04, 17:20
|
|
Registered User
|
|
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
|
|
pls post your script and a smple data file.
|
|

05-01-04, 17:41
|
|
Registered User
|
|
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$
|
|

05-01-04, 17:51
|
|
Registered User
|
|
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'
|
|

05-01-04, 17:53
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 8
|
|
Yes, it is Solaris. I will try nawk.
Thanks for all your help.
Chris
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|