LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1180|回复: 6

关于对AWK的結果排序问题,希望lastart老大能再次指点。。

[复制链接]
发表于 2009-10-15 10:52:54 | 显示全部楼层 |阅读模式
$ cat -A lab5.data1
Mike Harrington510) 548-1278:250:100:175
Christian Dobbi408) 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:(408) 926-3456:250:168:200
Elizabeth Stach:(916) 440-1763:175:75:300

要求写脚本达到以下效果:

NAME                         PHONE                   Jan  | Feb  | Mar  | Total Donated
________________________________________________________________________
Mike Harrington        (510) 548-1278    250   100     175        525
Christian Dobbi         (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 Stach          (916) 440-1763     175  75        300        550
_________________________________________________________________________

            SUMMARY
_________________________________________________________________________
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--(408) 926-3456
                 Elizabeth Stach--(916) 440-1763

关键的是红色部分。现在只能将over 500的人名字和电话打印出来,結果如下:
Elizabeth Stachelin--(916) 440-1763
John Goldenrod--(916) 348-4278
Archie McNichol--(206) 548-1348
Mike Harrington--(510) 548-1278
Tom Savage--(408) 926-3456
Guy Quigley--(916) 343-6410
Dan Savage--(406) 298-7744


这个結果MS差不多,但是我发现要求是需要把以姓为关键字进行降序排序。。

请各位老大指点一下,特别是lastart老大:Coffee::Coffee:!
 楼主| 发表于 2009-10-15 10:55:41 | 显示全部楼层
我的代码:
BEGIN{FS=":"
        print "\n"
        print "\t\t***CAMPAIGN 1998 CONTRIBUTIONS***"
        print "---------------------------------------------------------"
        print "NAME\tPHONE\t\tJAN |  FEB |  MAR | Total Donated"
        print "---------------------------------------------------------"
        hight = 0
        lowest = 10000
        ht = 0
        user = $1
}
{$6 = $3+$4+$5;print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}
{total+=$6;average=total/12}
{if($5 > hight) hight = $5}
{if($3 < lowest) lowest = $3}
{if($6 > ht) {ht = $6;user = $1}}
$6 > 500{over500[$0]=$1"--"$2}

END{
        print "---------------------------------------------------------"
        print "\t\tSUMARY"
        print "---------------------------------------------------------"
        printf "The campaign received a total of $" "%2.2f\n",total" for this quarter\."
        printf "The average donation for the 12 contributors was $" "%2.2f\n",average"\."
        print "The highest contribution was $"hight"."
        print "The lowest contribution was $"lowest"."
        print "The highest total contribution was $" ht " made by "user"."
        print "  ***THANKS " user"  ***"
        print "The following people donated over %500 to the campaign."
        print "They are eligible for the quarterly drawing!!"
        print "Listed are their names (sorted by last names) and phone numbers:"
        for (a in over500) print over500[a]
回复 支持 反对

使用道具 举报

发表于 2009-10-15 19:05:35 | 显示全部楼层
  1. #!/usr/bin/awk -f
  2. BEGIN{FS=":"
  3. print "\n"
  4. print "\t\t***CAMPAIGN 1998 CONTRIBUTIONS***"
  5. print "---------------------------------------------------------"
  6. print "NAME\tPHONE\t\tJAN | FEB | MAR | Total Donated"
  7. print "---------------------------------------------------------"
  8. hight = 0
  9. lowest = 10000
  10. ht = 0
  11. user = $1
  12. [color="Red"]t_over500=0[/color]
  13. }
  14. {$6 = $3+$4+$5;print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}
  15. {total+=$6;average=total/12}
  16. {if($5 > hight) hight = $5}
  17. {if($3 < lowest) lowest = $3}
  18. {if($6 > ht) {ht = $6;user = $1}}
  19. [color="Red"]$6 > 500{split($1,name," ");
  20.         for(i_over500=0;i_over500<t_over500 && name[2] >= over500[i_over500,1];i_over500++);
  21.         for(j_over500=t_over500;j_over500>i_over500;j_over500--){over500[j_over500,0]=over500[j_over500-1,0];
  22.                 over500[j_over500,1]=over500[j_over500-1,1];}
  23.         over500[i_over500,0]=$1"--"$2;
  24.         over500[i_over500,1]=name[2];
  25.         t_over500++;
  26. }[/color]
  27. END{
  28. print "---------------------------------------------------------"
  29. print "\t\tSUMARY"
  30. print "---------------------------------------------------------"
  31. printf "The campaign received a total of $" "%2.2f\n",total" for this quarter\."
  32. printf "The average donation for the 12 contributors was $" "%2.2f\n",average"\."
  33. print "The highest contribution was $"hight"."
  34. print "The lowest contribution was $"lowest"."
  35. print "The highest total contribution was $" ht " made by "user"."
  36. print " ***THANKS " user" ***"
  37. print "The following people donated over [color="Red"]$[/color]500 to the campaign."
  38. print "They are eligible for the quarterly drawing!!"
  39. print "Listed are their names (sorted by last names) and phone numbers:"
  40. [color="Red"]for (i=0;i<t_over500;i++) print over500[i,0][/color]
  41. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2009-10-16 03:53:57 | 显示全部楼层
用“指针”的思想,插入数据时不用移动。--好像叫“链表”
姓一样时比名。
  1. #!/usr/bin/awk -f
  2. BEGIN{FS=":"
  3. print "\n"
  4. print "\t\t***CAMPAIGN 1998 CONTRIBUTIONS***"
  5. print "---------------------------------------------------------"
  6. print "NAME\tPHONE\t\tJAN | FEB | MAR | Total Donated"
  7. print "---------------------------------------------------------"
  8. hight = 0
  9. lowest = 10000
  10. ht = 0
  11. user = $1
  12. [color="Red"]over500[0,0]=0[/color]
  13. }
  14. {$6 = $3+$4+$5;print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}
  15. {total+=$6;average=total/12}
  16. {if($5 > hight) hight = $5}
  17. {if($3 < lowest) lowest = $3}
  18. {if($6 > ht) {ht = $6;user = $1}}
  19. [color="Red"]$6 > 500{split($1,name," ");
  20.         for(p=0; over500[p,0] != 0 && (name[2] > over500[over500[p,0],2] || name[2] == over500[over500[p,0],2] && name[1] > over500[over500[p,0],3]); p=over500[p,0]);
  21.         over500[$0,0]=over500[p,0];
  22.         over500[$0,1]=$1"--"$2;
  23.         over500[$0,2]=name[2];
  24.         over500[$0,3]=name[1];
  25.         over500[p,0]=$0;
  26. }[/color]
  27. END{
  28. print "---------------------------------------------------------"
  29. print "\t\tSUMARY"
  30. print "---------------------------------------------------------"
  31. printf "The campaign received a total of $" "%2.2f\n",total" for this quarter\."
  32. printf "The average donation for the 12 contributors was $" "%2.2f\n",average"\."
  33. print "The highest contribution was $"hight"."
  34. print "The lowest contribution was $"lowest"."
  35. print "The highest total contribution was $" ht " made by "user"."
  36. print " ***THANKS " user" ***"
  37. print "The following people donated over $500 to the campaign."
  38. print "They are eligible for the quarterly drawing!!"
  39. print "Listed are their names (sorted by last names) and phone numbers:"
  40. [color="Red"]for (p=over500[0,0];p != 0; p=over500[p,0]) print over500[p,1]
  41. }[/color]
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-10-19 08:55:43 | 显示全部楼层
非常感谢:Coffee:
原来这么高深呀...
我还以为看完unix shell 范例精解就能搞定..

C语言还没学完..
对于这些代码还是有点困惑...

再次感谢!!:thank
回复 支持 反对

使用道具 举报

发表于 2009-10-19 17:30:40 | 显示全部楼层
这是编程的基础知识,跟用什么语言没关系。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-10-22 16:19:09 | 显示全部楼层
Post by lastart;2038063
这是编程的基础知识,跟用什么语言没关系。


哦...受教了..

我现在是对编程方面不想深入..恐怕以后也深入不了了..
回复 支持 反对

使用道具 举报

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

本版积分规则

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