Uinx Shell by Example,3rd Edition第7章 awk编程 章节的答案

使用命令awk -f scriptname filename来运行

如第一个脚本名是bigawk.sc1 文件名是datafile    #datafile的内容和前面的内容是一样的,这里就不贴了,可以去看我上一节的
那么实际命令: awk -f bigawk.sc1 datafile

再说明下最后一个是通过命令行去传递的参数

实际命令是:awk -f myfunc.sc 3 datafile   

这里的可以通过数字1,2,3来指定你想要得到当月的所有人的贡献钱的平均值。

 

 

 

 

#Comment : This is awk script
#Name    : bigawk.sc1
#Position: 7.5Review
#Function: Output table by use datafile

BEGIN{ FS=":"; _2ndmax = 0; _1stmin = 1000;
printf "/t/t/t***CAMPAIGN 1998 CONTRIBUTIONS***/t/t/t/t/t/t/n"
printf "----------------------------------------------------------------------------/n"
printf "NAME/t/t   PHONE/t/tJan  |  Feb  |  Mar  |  Total Donated/n"
printf "----------------------------------------------------------------------------/n"
}
{total=$3+$4+$5; sum+=total; printf "%-19s%-19s%6.2f%9.2f%9.2f%9.2f/n",$1,$2,$3,$4,$5,total}
{_2ndmax = (_2ndmax > $4) ? _2ndmax : $4 }
{_1stmin = (_1stmin < $3) ? _1stmin : $3 }
END{
printf "----------------------------------------------------------------------------/n"
printf "/t/t/t/t SUMMARY/n"
printf "----------------------------------------------------------------------------/n"
printf "The campaign received a total of $%5.2f for this quarter./n",sum
printf "The average donation for the %s contributors was $%5.2f./n",NR,sum/NR
printf "The Second month highest contribution was $%5.2f./n",_2ndmax
printf "The First month lowest contribution was $%5.2f./n",_1stmin
}

 

 

#Comment : This is awk script
#Name    : bigawk.sc2
#Position: 7.13Review
#Function: Output table by use datafile

BEGIN{ FS=":";
printf "/t/t/t  ***FIRST QUARTERLY REPORT***/N"
printf "/t/t/t***CAMPAIGN 1998 CONTRIBUTIONS***/t/t/t/t/t/t/n"
printf "----------------------------------------------------------------------------/n"
printf "NAME/t/t   PHONE/t/tJan  |  Feb  |  Mar  |  Total Donated/n"
printf "----------------------------------------------------------------------------/n"
}
{total=$3+$4+$5; sum+=total; printf "%-19s%-19s%6.2f%9.2f%9.2f%9.2f/n",$1,$2,$3,$4,$5,total}

#find first quarterly who was contribution total max
{ if(maxtotal<total){
    maxtotal=total
    thankname=$1
  }
}
#find those donated over 500 and storage in array
{
if(total > 500){
    nphrecord[i++]=$1"--"$2
}   

}
END{
printf "----------------------------------------------------------------------------/n"
printf "/t/t/t/t SUMMARY/n"
printf "----------------------------------------------------------------------------/n"
printf "The campaign received a total of $%5.2f for this quarter./n",sum
printf "The average donation for the %s contributors was $%5.2f./n",NR,sum/NR
printf "The highest total contribution was $%5.2f made by %s/n",maxtotal,/
thankname

printf "/t/t/t***THANKS "
split(thankname,namearray," ")
printf "%s***/n",namearray[1]

printf "The following people donated over $500 to the campaign./n"
printf "They are eligible for the quarterly drawing!!/n"
printf "Listed are their names (sorted by last names) and phone numbers:/n"

#use special for print those people who over donated 500 info.
for(i in nphrecord){
    printf "/t%s/n",nphrecord[i]
}

printf "/t  Thanks to all of you for your continued support!!/n"
}

 

 

#Comment:This is awk script
#Position:7.15 Review
#NAME   :myfunc.sc
#Function:use argument determine month to count average of that month donated

BEGIN{FS=":";month=ARGV[1];delete ARGV[1];sum=0}
function month_donate(m){
sum+=$(m+2)
}
{ month_donate(month)}
END{print "in " month " average donated was " sum/NR}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值