0

我正在尝试使用awk做工资单报告,但我不太确定如何去做。已尝试以下方法,但似乎无法正常工作。我被卡住了,因为我编写的代码设法理清了“受薪”,但仍然列出了其他数据,而不仅仅是姓名和工资。

编辑:我已经尝试了计算部分..但也不知道它是如何工作的

需要结果显示为:

1)整理类型“受薪”,每小时和委托

例如:

Salaried:
Frank    $2333
Mary     $1111

Total salary: $3444
----------------------
Grand Total: $3444

代码:

 echo "***** payroll report ****"
 awk -F',' '{print $2}' | grep "Salaried" $PAYROLL

 totalcost=0
   salariedcost=0
   for i in `grep $j $PAYROLL | cut -d "," -f6`
   do
    let "salariedcost = salariedcost + $i"
   done
    echo "Salaried Cost: \$${salariedcost}"
    let "totalcost = totalcost + salariedcost"
   echo "Total Cost: \$$totalcost"

   echo -en "Hit [Enter] to return to main menu..."
   read  

.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]
4

2 回答 2

1

尝试使用awk

awk -F, 'BEGIN {print "Salaried:"} $5=="Salaried"{total+=$6; printf "%s\t$%s\n", $2, $6} END {printf "Total salary: $%s", total}' $PAYROLL

输出:

Salaried:
Frank   $2333
Mary    $1111
Total salary: $3444
于 2013-11-02T17:00:26.083 回答
0
awk -F',' '{print $2}' | grep "Salaried" $PAYROLL

这告诉grep打开名为 in 的文件$PAYROLL,搜索字符串Salaried,并在找到它时打印整行。grep退出,awk并被 a 杀死SIGPIPE。你可能想要什么:

awk -F, '{print $2}' "$PAYROLL" | grep Salaried 

注意引用的细微变化。

但是awk模式匹配就像grep

awk -F, '/Salaried/{print $2}' "$PAYROLL"

对于整个程序,你会想要这样的东西:

awk -F, '
# Before processing the first line, print out the header
BEGIN {
    print "Salaried:"
}
# Lines matching Salaried
/Salaried/ {
    # Print name <tab> salary
    print $2 "\t$" $6

    # Add their salary to our salary total
    salaries += $6
}

# Every line, add cost to total
{ 
    total += $6
}

# After processing all lines
END {
    # Print the salary total, separator, and grand total.
    print "Total Salary: $" salaries
    print "--------------------"
    print "Grand total: $" total
}' file.txt
于 2013-11-02T16:15:01.380 回答