使用命令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}