免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1577 | 回复: 5
打印 上一主题 下一主题

用awk处理一个数据库文件: ) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-12-05 08:15 |只看该作者 |倒序浏览
初学awk,来这里寻宝,无意看见了一个数据处理问题,发觉是我正在看的一本书上的习题,兴奋之至,开始了编脚本,最终结果是给弄出来了,但有的地方处理的有些牵强,故来这里请教各位大虾了: )
      题目(我做了些改动: ) ):
Mike Harrington510) 548-1278250100175
Christian Dobbins40 538-235815590201
Susan Dalsass206) 654-62792506050
Archie McNichol206) 548-1348250:$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 $300.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!!

论坛徽章:
0
2 [报告]
发表于 2004-12-05 08:17 |只看该作者

用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"
}

论坛徽章:
0
3 [报告]
发表于 2004-12-05 08:20 |只看该作者

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

在最后处理排序输出时老是不能使“Thanks.............”那句输出到最后,只好用了一个牵强的方法得出结果,应该如何更好的解决它阿

论坛徽章:
0
4 [报告]
发表于 2004-12-05 12:24 |只看该作者

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

看得晕晕,请把 SUMMARY里每行数据的算法讲一下,别让人还要去研究你的算法再去做题目

论坛徽章:
0
5 [报告]
发表于 2004-12-05 22:34 |只看该作者

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

# 初始化模块
BEGIN { FS=":"
        print "\t\t\t***CAMPAIGN 1998 CONTRIBUTIONS***"
        print "---------------------------------------------------------------------------------"
        print "NAME\t\t\tPHONE\t Jan | Feb | Mar | Total Donated"
        print "---------------------------------------------------------------------------------"
    }
    {
        gsub(/\$/,""
# gsub将$全部清除掉,使下面能够进行数值运算。
        $6=$3+$4+$5
# 格式化输出所有数据
        printf("%-20s%-16s%-10.2f%.2f\t%.2f\t %10.2f\n",$1,$2,$3,$4,$5,$6)
    }

# 计算所有donation的和。
        {total+=$6}

# 下面是排序操作
    {
        for(i=3;i<NF-1;i++) {

# 将i域赋值到数组dona               
                dona[i-1]=$i

# dona[0]将永远保存最大值
                if(dona[0]<dona[i-1]) dona[0]=dona[i-1]

# 若dona[1]则将dona[2]赋值到dona[1]
                if(dona[1]==0) dona[1]=dona[2]

# dona[1]将永远保存最小值
                if(dona[1]>dona[i-1]) dona[1]=dona[i-1];
          }
    }

    {
# 下面将得到最大贡献者的名字及相应的电话号码
        if(max<$6) {
                name=$1;
                max=$6;
           }

# 下面将得到贡献大于500的人的名字及其相应的电话号码
        if($6>500) {
                j++;
                group[j]=$1"--";
                telephone[j]=$2;
           }

    }

# 结束模块
END {
        print "---------------------------------------------------------------------------------"
        print "\t\t\t\t SUMMARY"
        print "---------------------------------------------------------------------------------"

# 输出所有donation的和。
        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])

# 将name分割到数组nick
        split(name,nick," "
       
# 输出全部贡献额的最大值及其名字
        printf("The highest total contribution was $%.2f made by %s\n",max,name)

# 输出该人的first name
        printf("\t\t\t\t ***THANKS %s ***\n",nick[1])

# 输出贡献值大于500的所有人的名字及其telephone,要求按last name的字母顺序输出
        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]
        group[j+1]="\tThanks to all of you for your continued support!!"

# 下面是对group数组进行排序输出;这个地方我试过通过赋值到新的域然后再排序输出,但最后的那句"thanks ..."还是不能在最后一行输出

,只能在末尾同样加入 | "sort +1 -2"才得到结果,但实质还是和数组的方法差不多,不知道该如何更好地解决。
        for(i=1;i<=j+1;i++) print "\t"grouptelephone | "sort +1 -2"
    }

论坛徽章:
0
6 [报告]
发表于 2004-12-05 22:35 |只看该作者

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

# 初始化模块
BEGIN { FS=":"
        print "\t\t\t***CAMPAIGN 1998 CONTRIBUTIONS***"
        print "---------------------------------------------------------------------------------"
        print "NAME\t\t\tPHONE\t Jan | Feb | Mar | Total Donated"
        print "---------------------------------------------------------------------------------"
    }
    {
        gsub(/\$/,""
# gsub将$全部清除掉,使下面能够进行数值运算。
        $6=$3+$4+$5
# 格式化输出所有数据
        printf("%-20s%-16s%-10.2f%.2f\t%.2f\t %10.2f\n",$1,$2,$3,$4,$5,$6)
    }

# 计算所有donation的和。
        {total+=$6}

# 下面是排序操作
    {
        for(i=3;i<NF-1;i++) {

# 将i域赋值到数组dona               
                dona[i-1]=$i

# dona[0]将永远保存最大值
                if(dona[0]<dona[i-1]) dona[0]=dona[i-1]

# 若dona[1]=0则将dona[2]赋值到dona[1]
                if(dona[1]==0) dona[1]=dona[2]

# dona[1]将永远保存最小值
                if(dona[1]>dona[i-1]) dona[1]=dona[i-1];
          }
    }

    {
# 下面将得到最大贡献者的名字及相应的电话号码
        if(max<$6) {
                name=$1;
                max=$6;
           }

# 下面将得到贡献大于500的人的名字及其相应的电话号码
        if($6>500) {
                j++;
                group[j]=$1"--";
                telephone[j]=$2;
           }

    }

# 结束模块
END {
        print "---------------------------------------------------------------------------------"
        print "\t\t\t\t SUMMARY"
        print "---------------------------------------------------------------------------------"

# 输出所有donation的和。
        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])

# 将name分割到数组nick
        split(name,nick," "
       
# 输出全部贡献额的最大值及其名字
        printf("The highest total contribution was $%.2f made by %s\n",max,name)

# 输出该人的first name
        printf("\t\t\t\t ***THANKS %s ***\n",nick[1])

# 输出贡献值大于500的所有人的名字及其telephone,要求按last name的字母顺序输出
        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]
        group[j+1]="\tThanks to all of you for your continued support!!"

# 下面是对group数组进行排序输出;这个地方我试过通过赋值到新的域然后再排序输出,但最后的那句"thanks ..."还是不能在最后一行输出

,只能在末尾同样加入 | "sort +1 -2"才得到结果,但实质还是和数组的方法差不多,不知道该如何更好地解决。
        for(i=1;i<=j+1;i++) print "\t"grouptelephone | "sort +1 -2"
    }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP