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}

 

 

 

你可能感兴趣的:(Uinx Shell by Example,3rd Edition第7章 awk编程 章节的答案)