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:
BEGIN {
printf("%s \t %s \t %s \t %s \t %s \t %s \n", "Name", "Gross Pay", "State Tax", "Fed Tax", "Total Tax", "Net Pay");
}
$1 !~/^#/ {
grossPay = $4*$6;
grossPay += $5*$6*1.5;
if($2 == S) fedTaxRate = .15;
if($2 == M) fedTaxRate = .10;
if($3 == 0) stateTaxRate = .10;
if($3 > 0 && $3 <6) stateTaxRate = .05;
if($3 > 5 && $3 <10) stateTaxRate = .025;
if($3 == 10) stateTaxRate = 0;
totalTaxRate = 1 - fedTaxRate - stateTaxRate;
totalTax = totalTaxRate * grossPay;
stateTax = stateTaxRate * grossPay;
fedTax = fedTaxRate * grossPay;
netPay = grossPay - totalTax;
printf("%s \t %d \t %d \t %d \t %d \t %d \n", $1, grossPay, stateTax, fedTax, totalTax, netPay);
}
END {
}
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.