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

來源:互聯網
上載者:User

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

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.