Awk Problems

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 13
Reputation: Gerbilkit is an unknown quantity at this point 
Solved Threads: 0
Gerbilkit Gerbilkit is offline Offline
Newbie Poster

Awk Problems

 
0
  #1
Oct 23rd, 2009
Alright I admit, I hate AWK. Heck even my C++ class is making more sense at the moment. I have a strong suspicion that this is a language I will learn and never use again after this class, as everything else seems better/more intuitive. But maybe that's just me.

I'm supposed to be reading from a data file like this.
Brook,S,0,40,2,15
Shields,M,1,35,0,10
Smith,s,10,40,0.5,20

And output the data into a table like this
Name Gross Pay State Tax Fed Tax Total Tax Net Pay
Brook 645.00 64.50 96.75 161.25 483.75
Shields 350.00 17.50 31.50 49.00 301.00
Smith 815.00 0.00 40.75 40.75 774.25
Total 1810.00 82.00 169.00 251.00 1559.00

First field in the input file is name, then marital status, then regular hours worked, overtime hours worked, finally pay.

Specific calculations are irrelevant. My problem is not understanding awk.
My code is the following:
Shell Scripting Syntax (Toggle Plain Text)
  1. BEGIN {
  2. printf("%s \t %s \t %s \t %s \t %s \t %s \n", "Name", "Gross Pay", "State Tax", "Fed Tax", "Total Tax", "Net Pay");
  3. }
  4.  
  5. $1 !~/^#/ {
  6. grossPay = $4*$6;
  7. grossPay += $5*$6*1.5;
  8.  
  9. if($2 == S) fedTaxRate = .15;
  10. if($2 == M) fedTaxRate = .10;
  11.  
  12. if($3 == 0) stateTaxRate = .10;
  13. if($3 > 0 && $3 <6) stateTaxRate = .05;
  14. if($3 > 5 && $3 <10) stateTaxRate = .025;
  15. if($3 == 10) stateTaxRate = 0;
  16.  
  17. totalTaxRate = 1 - fedTaxRate - stateTaxRate;
  18. totalTax = totalTaxRate * grossPay;
  19. stateTax = stateTaxRate * grossPay;
  20. fedTax = fedTaxRate * grossPay;
  21. netPay = grossPay - totalTax;
  22.  
  23. printf("%s \t %d \t %d \t %d \t %d \t %d \n", $1, grossPay, stateTax, fedTax, totalTax, netPay);
  24. }
  25.  
  26. END {
  27.  
  28. }
I admit I have absolutely no clue what $1 !~/^#/ { does, I found it online and put it in since before that I was getting syntax errors everywhere. Right now if I run this as it is instead of printing out the data, $1 prints out the whole input line and then every numeric variable is a 0.

Any help understanding where I went wrong, and what I need to do?
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,283
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 583
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
Oct 25th, 2009
Your file has 6 fields in it and you only gave 5. I'm guessing you left the tax rate out. Can you give an example of one line of sample data, all of the calculations, and the expected output?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 13
Reputation: Gerbilkit is an unknown quantity at this point 
Solved Threads: 0
Gerbilkit Gerbilkit is offline Offline
Newbie Poster
 
0
  #3
Oct 25th, 2009
Alright so here is a sample line of input.

Brook,S,0,40,2,15

Here are the calculations that should be performed

if ($2==S) // marital status is single so federal tax rate is 15%

multiple if statements, deductions are 0 so there are no deductions the state tax rate is 10%

Regular hours worked are 40, times 15 (the pay) is $600 + overtime hours worked (2) times pay (15) times 1.5 (overtime pay is 1.5 times greater)

Output should be
Brook 645 64.5 90 154.5 445.5
That is name, gross pay, state tax, federal tax, total tax, net pay

The actual output is
Brook,S,0,40,2,15 0 0 0 0 0

As I said I have no clue what #
$1 !~/^#/ { means, I found it on the internet and popped it in since it made lots of syntax errors go away.

After printing out the output of those users, I will need to print a total at the end of the file. At the moment I can't even get each line to print though.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,037
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
0
  #4
Oct 25th, 2009
Originally Posted by Gerbilkit View Post
As I said I have no clue what #
$1 !~/^#/ { means, I found it on the internet and popped it in since it made lots of syntax errors go away.
Your logic for including the matching pattern escapes me, however I can explain it to you.
This '$1 ~/^#/' tells awk to search for any line that in the first field the beginning of the line starts with a #
Kind of a redundancy since /^#/ can only happen in the beginning of a line.
The opposite is what you have: '$1 !~/^#/' . Search in any line that doesn't have # at the beginning of a line in the first field.
Does it make any sense for you want to do? Nope.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC