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.

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:

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);



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?

8 Years
Discussion Span
Last Post by Aia

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?


Alright so here is a sample line of input.


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. :P

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.


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. :P

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.