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.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > decimals in awk

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-01-04, 00:02
kcryss kcryss is offline
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
Reply With Quote
  #2 (permalink)  
Old 05-01-04, 11:29
vgersh99 vgersh99 is offline
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
Reply With Quote
  #3 (permalink)  
Old 05-01-04, 12:25
kcryss kcryss is offline
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.
Reply With Quote
  #4 (permalink)  
Old 05-01-04, 16:48
vgersh99 vgersh99 is offline
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
Reply With Quote
  #5 (permalink)  
Old 05-01-04, 17:09
kcryss kcryss is offline
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

?
Reply With Quote
  #6 (permalink)  
Old 05-01-04, 17:15
kcryss kcryss is offline
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
Reply With Quote
  #7 (permalink)  
Old 05-01-04, 17:20
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
pls post your script and a smple data file.
Reply With Quote
  #8 (permalink)  
Old 05-01-04, 17:41
kcryss kcryss is offline
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$
Reply With Quote
  #9 (permalink)  
Old 05-01-04, 17:51
vgersh99 vgersh99 is offline
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'
Reply With Quote
  #10 (permalink)  
Old 05-01-04, 17:53
kcryss kcryss is offline
Registered User
 
Join Date: Apr 2004
Posts: 8
Yes, it is Solaris. I will try nawk.

Thanks for all your help.

Chris
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On