awk 數據處理實例--數據庫文件處理
[haibin.xie@test ~]$ cat db
Mike Harrington:(510)548-1278:250:100:175
Christian Dobbins:(408)538-2358:155:90:201
Susan Dalsass:(206)654-6279:250:60:50
Archie McNichol:(206)548-1348:250:100:175
Jody Savage:(206)548-1278:15:188:150
Guy Quigley:(916)343-6410:250:100:175
Dan Savage:(406)298-7744:450:300:275
Nancy McNeil:(206)548-1278:250:80:75
John Goldenrod:(916)348-4278:250:100:175
Chet Main:(510)548-5258:50:95:135
Tom Savage:(408)926-3456:250:168:200
Elizabeth Stachelin:(916)440-1763:175:75:300
編寫awk腳本,使其如下輸出
[haibin.xie@test ~]$ awk -f gawk.sc2 db
***GAMPATGN 2000 CONTRIBUTIONS***
--------------------------------------------------------------------
NAME PHONE Jan| Feb| Mar| Total Donated
--------------------------------------------------------------------
Mike Harrington (510)548-1278 250 100 175 525
Christian Dobbins (408)538-2358 155 90 201 446
Susan Dalsass (206)654-6279 250 60 50 360
Archie McNichol (206)548-1348 250 100 175 525
Jody Savage (206)548-1278 15 188 150 353
Guy Quigley (916)343-6410 250 100 175 525
Dan Savage (406)298-7744 450 300 275 1025
Nancy McNeil (206)548-1278 250 80 75 405
John Goldenrod (916)348-4278 250 100 175 525
Chet Main (510)548-5258 50 95 135 280
Tom Savage (408)926-3456 250 168 200 618
Elizabeth Stachelin (916)440-1763 175 75 300 550
--------------------------------------------------------------------
SUMMARY
--------------------------------------------------------------------
The campaign received a total of $6137.00 for this quarter.
The average donation for the 12 contributors was $511.42.
The highest total contribution was $1025.00 made by Dan Savage.
***THANKS Dan***
The following people donated over $500 to the campaign.
They are eligible for the quarterly drawing!!
Listed are their names (sorted by last names) and phone numbers:
Thanks to all of you for your continued support!!
John Goldenrod--(916)348-4278
Mike Harrington--(510)548-1278
Archie McNichol--(206)548-1348
Guy Quigley--(916)343-6410
Dan Savage--(406)298-7744
Tom Savage--(408)926-3456
Elizabeth Stachelin--(916)440-1763
腳本如下:
[haibin.xie@test ~]$ cat gawk.sc2
BEGIN{FS=":";maxitem=0;
printf ("%50s\n%s\n%s\t\t\t%s\t\t%s\t%s\t%s\t%s\n%s\n",
"***GAMPATGN 2000 CONTRIBUTIONS***",
"--------------------------------------------------------------------",
"NAME","PHONE","Jan|","Feb|","Mar|","Total Donated",
"--------------------------------------------------------------------")}
#{printf ("%-20s",$1,"%s\t",$2,"%s\t",$3,"%s\t",$4,"%s\t",$5,"%s\n",$3+$4+$5)}
{$6=$3+$4+$5;printf ("%-20s%s\t%s\t%s\t%s\t%s\n",$1,$2,$3,$4,$5,$6);sum+=$6;
if ($6 > maxitem) {maxitem=$6;maxman=$1}
if ($6 > 500) {a[i++]=$1"--"$2}}
END{printf("%s\n%35s\n%s\n",
"--------------------------------------------------------------------",
"SUMMARY",
"--------------------------------------------------------------------")
printf ("The campaign received a total of $%.2f for this quarter.\n",sum)
printf ("The average donation for the 12 contributors was $%.2f.\n",sum/12)
printf ("The highest total contribution was $%.2f made by %s.\n",maxitem,maxman)
printf ("%40s\n","***THANKS "substr(maxman,1,index(maxman," ")-1)"***")
printf ("%s\n%s\n%s\n","The following people donated over $500 to the campaign.",
"They are eligible for the quarterly drawing!!",
"Listed are their names (sorted by last names) and phone numbers:")
{for( item in a ) printf "%-s\n"," "a[item] | "sort -k 2"}
printf ("%s\n","\tThanks to all of you for your continued support!!")
}
阅读(1099) | 评论(0) | 转发(0) |