awk -v hpd=8 '
{ printf "%s\t%s\t%s\t$%.2f/hr\t$%.2f/day\n", $1, $2, $3, $4, $4 * hpd;
ht += $4; # hourly total
};
END {
printf "\nWorkers earned a combined $%.2f per hour\n", ht
}
' input.txt
Adam Adamson Accounting $20.00/hr $160.00/day
Iver Iverson InfoTech $50.00/hr $400.00/day
Cary Caryson ChiefExecutive $200.00/hr $1600.00/day
Mary Maryson Maintenance $15.00/hr $120.00/day
Stan Stanson SalesDept. $10.00/hr $80.00/day
Scot Scotson SalesDept. $10.00/hr $80.00/day
Eric Ericson Executive $100.00/hr $800.00/day
Enid Enidson Executive $100.00/hr $800.00/day
Maye Mayeson Maintenance $15.00/hr $120.00/day
Axel Axelson Accounting $21.00/hr $168.00/day
Pete Peteson PayrollDept. $15.50/hr $124.00/day
Mick Mickson Marketing $12.00/hr $96.00/day
Iris Irisson InfoTech $55.00/hr $440.00/day
Hank Hankson HumanRes $42.42/hr $339.36/day
Workers earned a combined $665.92 per hour
The first block of code (i.e. inside the first pair of {
and }
curly braces) is run for every line of the input file (input.txt
in this case). The END {...}
block is only run once after all input has been read and processed.
You can change the number of hours per day by changing the hpd=8
part of the command line. I could have hard-coded it to 8, but I thought it was more interesting to make it a variable.
As an exercise, see if you can modify the final printf
statement so that it also prints a daily total. Figuring this out will help you to understand what the printf statement is doing. Output should be something like:
Workers earned a combined $665.92 per hour or $5327.36 per day