BEGIN{FS=","; t1 = 0; n = 0; avg = 0; sum=0; printf("%-10s %9s\n%-10s %9s","Name", "Average", "----", "-------") }
{
if ($3 > 0){
t1 += $3;
n++;
}
sum = 0;
if($3 > 0 && $4 > 0 && $5 >0){
sum = $3 + $4 + $5;
avg = sum / 3;
}
else if($3 < 0 && $4 > 0 && $5 >0){
sum = $4 + $5;
avg = sum / 2;
}
else if($3 > 0 && $4 < 0 && $5 >0){
sum = $3 + $5;
avg = sum / 2;
}
else if($3 < 0 && $4 > 0 && $5 < 0){
sum = $3 + $4;
avg = sum / 2;
}
printf("\n%-10s %7.2f", $1, avg);
}
END{
printf("-------------------\nAverage for Test ! : %f.2", (t1/n));
}
正文部分命令应忽略第一条记录。
答案1
您可以通过将记录编号的测试添加到您的块来完成此操作:
NR > 1 {
if ($3 > 0){
t1 += $3;
n++;
}
...