QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 888|回复: 2

用awk处理一个数据库文件: )

[复制链接]
发表于 2004-12-3 21:56:10 | 显示全部楼层 |阅读模式
初学awk,找了个数据库文件,按要求编了个awk脚本,有些地方处理得还很牵强,还望各位多多指教: )
(数据文件我做了些修改)
题目:
Mike Harrington510) 548-1278:$250:$100:$175
Christian Dobbins40 538-2358:$155:$90:$201
Susan Dalsass206) 654-6279:$250:$60:$50
Archie McNichol206) 548-1348:$250:$100:$175
Jody Savage206) 548-1278:$15:$188:$150
Guy Quigley916) 343-6410:$250:$100:$175
Dan Savage406) 298-7744:$450:$300:$275
Nancy McNeil206) 548-1278:$250:$80:$75
John Goldenrod916) 348-4278:$250:$100:$175
Chet Main510) 548-5258:$50:$95:$135
Tom Savage:(40 926-3456:$250:$168:$200
Elizabeth Stachelin:(916) 440-1763:$175:$75:$300
要求输出成

代码:

                    ***CAMPAIGN 1998 CONTRIBUTIONS***
----------------------------------------------------------------------
NAME                 PHONE            Jan  |  Feb  |  Mar | Total Donated
----------------------------------------------------------------------
Mike Harrington     (510) 548-1278    250.00   100.00  175.00   525.00
Christian Dobbins   (40 538-2358    155.00    90.00  201.00   446.00
Susan Dalsass       (206) 654-6279    250.00    60.00   50.00   360.00
Archie McNichol     (206) 548-1348    250.00   100.00  175.00   525.00
Jody Savage         (206) 548-1278     15.00   188.00  150.00   353.00
Guy Quigley         (916) 343-6410    250.00   100.00  175.00   525.00
Dan Savage          (406) 298-7744    450.00   300.00  275.00  1025.00
Nancy McNeil        (206) 548-1278    250.00    80.00   75.00   405.00
John Goldenrod      (916) 348-4278    250.00   100.00  175.00   525.00
Chet Main           (510) 548-5258     50.00    95.00  135.00   280.00
Tom Savage          (40 926-3456    250.00   168.00  200.00   618.00
Elizabeth Stacheli  (916) 440-1763    175.00    75.00  300.00   550.00
----------------------------------------------------------------------
                              SUMMARY
----------------------------------------------------------------------
The campaign received a total of $6137.00 for this quarter.
The average donation for the 12 contributors was $511.42.
The highest contribution was $450.00.
The lowest contribution was $15.00.
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:
        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--(40 926-3456
        Elizabeth Stachelin--(916) 440-1763
                Thanks to all of you for your continued support!!
 楼主| 发表于 2004-12-3 21:58:02 | 显示全部楼层

我处理文件的awk脚本是:

BEGIN { FS=":"
                print "\t\t\t***CAMPAIGN 1998 CONTRIBUTIONS***"
                print "---------------------------------------------------------------------------------"
                print "NAME\t\t\tPHONE\t     Jan  |   Feb    |    Mar |   Total Donated"
                print "---------------------------------------------------------------------------------"
}
        {
        gsub(/\$/,"")
        $6=$3+$4+$5
        printf("%-20s%-16s%-10.2f%.2f\t%.2f\t %10.2f\n",$1,$2,$3,$4,$5,$6)
    }
    {total+=$6}
        {
        for(i=3;i<NF-1;i++)  {
                dona[i-1]=$i
                if(dona[0]<dona[i-1])   dona[0]=dona[i-1]
                if(dona[1]==0) dona[1]=dona[2]
                if(dona[1]>dona[i-1])   dona[1]=dona[i-1];
                    }
    }
        {
        if(max<$6) {
                name=$1;
                max=$6;
             }
        if($6>500) {
                j++;
                group[j]=$1"--";
                telephone[j]=$2;
            }

   }

END {
                print "---------------------------------------------------------------------------------"
                print "\t\t\t\t      SUMMARY"
                print "---------------------------------------------------------------------------------"
                printf("The campaign received a total of $%.2f for this quarter.\n",total)
                printf("The average donation for the %d contributors was $%.2f.\n",NR,total/NR)
                printf("The highest contribution was $%.2f.\n",dona[0])
                printf("The lowest contribution was $%.2f.\n",dona[1])
                split(name,nick," ")
                printf("The highest total contribution was $%.2f made by %s\n",max,name)
                printf("\t\t\t\t ***THANKS %s ***\n",nick[1])
                print "The following people donated over $500 to the campaign!\nThey are eligible for the quarterly drawing !!\nListed are their names ( sorted by last names ) and phone numbers:"
                group[j+1]="\tThanks to all of you for your continued support!!"
                for(i=1;i<=j+1;i++) print "\t"grouptelephone | "sort +1 -2"
}
回复

使用道具 举报

 楼主| 发表于 2004-12-3 22:05:19 | 显示全部楼层
其中有些地方处理的不够好,自己最不满意的地方是在输出最后一句“Thanks to .........”时不得不将其赋值到group[j+1]才得到结果,还得请教大虾帮我解决这个不太满意的问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-5 15:47 , Processed in 0.062420 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表