2

嗨,我想列出报告的总数。试图列出总数。但不确定使用什么代码将awk中的total salariedand组合在一起total hourly

请指教

结果:

Salaried:
Frank    $2333
Mary     $1111

Total salaried: $3444
Hourly:
Chris     $122

Total Hourly:122
----------------------
Grand Total: $3566

.txt 文件:

顺序如下:[id]、[name]、[title]、[phone]、[type]、[pay]

3,Frank,CFO,91111453,Salaried,2333
1,Mary,CEO,93424222,Salaried,1111
5,John,Sales user,9321312,Commission,9999
7,Chris,Admin,98888753,Hourly,122

代码 :

     #salaried
        awk -F, 'BEGIN {print "Salaried"} $5=="Salaried"{salaried+=$6; printf "%s\t\t\t\t$%s\n", $2, $6} END {printf "Total salaried payroll: \t$%s", salaried}' $PAYROLL
       echo
       echo
       #hourly
       awk -F, 'BEGIN {print "Hourly"} $5=="Hourly"{hourly+=$6; printf "%s\t\t\t\t$%s\n", $2, $6} END {printf "Total Hourly payroll: \t\t$%s", hourly}' $PAYROLL
       echo
       echo "Grand total: 
4

1 回答 1

1

像这样使用awk;

awk -F, '{t[$5]+=$6} END{for (i in t) print i, t[i]}' file
Hourly 122
Commission 9999
Salaried 3444

对于弗兰特总计:

awk -F, '{t[$5]+=$6} END{for (i in t) {sum+=t[i]; print i, t[i]} print "Grand Total: " sum}' file
Hourly 122
Commission 9999
Salaried 3444
Grand Total: 13565
于 2013-11-06T16:21:30.580 回答