linux awk高级应用实例
今天看到unixshell范例精解上有道awk的题目做了以后拿来和大家分享下
处理前的文档:
MikeHarrington:(510)548-1278:250:100:175
ChristianDobbins:(408)538-2358:155:90:201
SusanDalsass:(206)654-6279:250:60:50
ArchieMcNichol:(206)548-1348:250:100:175
JodySavage:(206)548-1278:15:188:150
GuyQuigley:(916)343-6410:250:100:175
DanSavage:(406)298-7744:450:300:275
NancyMcNeil:(206)548-1278:250:80:75
JohnGoldenrod:(916)348-4278:250:100:175
ChetMain:(510)548-5258:50:95:135
TomSavage:(408)926-3456:250:168:200
ElizabethStachelin:(916)440-1763:175:75:300
需要的结果如下:
***CAMPAIGN1998CONTRIBUTIONS***
Name PHone Jan| Feb| MAR| TotalDonated
---------------------------------------------------------------------------------
MikeHarrington (510)548-1278 250 100 175 525
ChristianDobbs (408)538-2358 155 90 201 446
SusanDalsass (206)654-6279 250 60 50 360
ArchieMcNichol (206)548-1348 250 100 175 525
JodySavage (206)548-1278 15 188 150 353
GuyQuigley (916)343-6410 250 100 175 525
DanSavage (406)298-7744 450 300 275 1025
NancyMcNeil (206)548-1278 250 80 75 405
JohnGoldenrod (916)348-4278 250 100 175 525
ChetMain (510)548-5258 50 95 135 280
TomSavage (408)926-3456 250 168 200 618
ElibethStachel (916)440-1763 175 75 300 550
SUMMARY
-----------------------------------------------------------------------------------
Thecampanreceivedatotalof$6137forthisquarter
averagedonationforthe12contributorswas$511.417.
Thehighestcontributionwas$450.
Thelowestcontributionwas$15.
代码如下:
BEGIN{FS=":";low1=300;low2=400;low3=500 OFS="\t" print"\t\t***CAMPAIGN1998CONTRIBUTIONS***\n" print"---------------------------------------------------------------------------------\n" print"Name\t\t\tPHone\t\t\tJan|\tFeb|\tMAR|\tTotalDonated" print"---------------------------------------------------------------------------------\n" } {tot=$3+$4+$5} {Ttot+=tot} {print$1,"\t"$2"\t\t"$3"\t"$4"\t"$5"\t"tot} {avg=Ttot/12} {high1=(high1>$3)?high1:$3} {high2=(high1>$4)?high1:$4} {high3=(high1>$5)?high1:$5} {max12=(high1>high2)?high1:high2} {max23=(high2>high3)?high2:high3} {Max=(max12>max23)?max12:max23} {low1=(low1<$3)?low1:$3} {low2=(low1<$4)?low1:$4} {low3=(low1<$5)?low1:$5} {min12=(low1<low2)?low1:low2} {min23=(low2<low3)?low2:low3} {Min=(min12<min23)?min12:min23} END{ print"-----------------------------------------------------------------------------------" print"\t\tSUMMARY" print"-----------------------------------------------------------------------------------" printf"Thecampanreceivedatotalof$";printfTtot;print"forthisquarter" printf"averagedonationforthe12contributorswas$";printfavg;print"." printf"Thehighestcontributionwas$";printfMax;print"." printf"Thelowestcontributionwas$";printfMin;print"." }