Chinaunix首页 | 论坛 | 博客
  • 博客访问: 265859
  • 博文数量: 54
  • 博客积分: 35
  • 博客等级: 民兵
  • 技术积分: 729
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-09 10:35
文章分类

全部博文(54)

文章存档

2014年(37)

2013年(17)

分类: LINUX

2014-01-16 11:07:53

原帖:处理前:
pear    mouse107
pear    mouse123
pear    mouse109
pear    mouse125
apple   cat123
pear    dog105
pear    dog101
apple   cat12
pear    dog104
apple   cat108
pear    dog11
apple   cat125
apple   cat106
pear    mouse108
pear    mouse106
apple   cat107
pear    dog103
apple   cat109

处理后:
apple: cat12,cat106-109,cat123,cat125
pear: dog11,dog101,dog103-105,mouse106-109,mouse123,mouse125

处理要求:
根据第一列分类,对第二列进行合并,连续的用破折号相连,不连续的用逗号隔开,按照ACSII码顺序对子类(例如pear的dog和mouse)进行排序。


点击(此处)折叠或打开

  1. #!/bin/awk -f
  2. {
  3.         m=gensub(/([^0-9]+).*/,"\\1",1,$2)
  4.         n=gensub(/[^0-9]+(.*)/,"\\1",1,$2)
  5.         t=$1" "sprintf("%s%10s",m,n)
  6.         a[t]=$1
  7.         b[t]=m
  8.         c[t]=n
  9. }
  10. END{
  11.         for(i=0;i++<asorti(c,d);){
  12.                 if(!k)printf "%s: ",a[d[i]]
  13.                 if(k&&k!=a[d[i]]){
  14.                         gsub(/-[^,]+-[^0-9]+/,"-",s)
  15.                         printf "%s\n%s: ",s,a[d[i]]
  16.                         s=""
  17.                 }
  18.                 k=a[d[i]]
  19.                 if(s){
  20.                         if(a[d[i]]==a[d[i-1]]){
  21.                                 if(b[d[i]]!=b[d[i-1]]){s=s","b[d[i]]c[d[i]];continue}
  22.                                 s=c[d[i]]-c[d[i-1]]==1?s"-"b[d[i]]c[d[i]]:s","b[d[i]]c[d[i]]
  23.                         }
  24.                 } else s=b[d[i]]c[d[i]]
  25.         }
  26.         gsub(/-[^,]+-[^0-9]+/,"-",s)
  27.         print s;
  28. }

点击(此处)折叠或打开

  1. #!/bin/awk -f
  2. {
  3.         match($2,/([^0-9]+)(.*)/,a)
  4.         b[$1][a[1]][a[2]]=a[2]
  5. }
  6. END{
  7.         for(i in b){
  8.                 printf i":\t"
  9.                 for(j=0;j++<asorti(b[i],c);){
  10.                         for(k=0;k++<asort(b[i][c[j]],d);){
  11.                                 t=t?d[k]-d[k-1]==1?t"-"c[j]d[k]:t","c[j]d[k]:c[j]d[k]
  12.                         }
  13.                         s=s?s","t:t
  14.                         t=""
  15.                 }
  16.                 gsub(/-[^,]+-[^0-9]+/,"-",s)
  17.                 print s
  18.                 s=""
  19.         }
  20. }

点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. my %h;
  3. map { push @{ $h{$_->[0]}{$_->[1]} }, $_->[2] } sort { $a->[2] <=> $b->[2] } map { [ /(\S+)\s+(\D+)(\d+)/ ] } <DATA>;
  4. for my $k1 ( sort keys %h ) {
  5.         print "$k1: ";
  6.         my $s1;
  7.         for my $k2 ( sort keys %{ $h{$k1} } ) {
  8.                 my $s2;
  9.                 for ( 0 .. $#{ $h{$k1}{$k2} } ) {
  10.                         my $t = "$k2$h{$k1}{$k2}[$_]";
  11.                         $s2 .= $h{$k1}{$k2}[$_+1] ? $h{$k1}{$k2}[$_+1] - $h{$k1}{$k2}[$_] == 1 ? "$t-" : "$t," : $t;
  12.                 }
  13.                 $s1 .= $s1 ? ",$s2" : $s2;
  14.         }
  15.         $s1 =~ s/-[^,]+-\D+/-/g;
  16.         print "$s1$/";
  17. }
  18. __DATA__
  19. pear mouse107
  20. pear mouse123
  21. pear mouse109
  22. pear mouse125
  23. apple cat123
  24. pear dog105
  25. pear dog101
  26. apple cat12
  27. pear dog104
  28. apple cat108
  29. pear dog11
  30. apple cat125
  31. apple cat106
  32. pear mouse108
  33. pear mouse106
  34. apple cat107
  35. pear dog103
  36. apple cat109


阅读(1179) | 评论(0) | 转发(1) |
0

上一篇:排序合并(二)

下一篇:图形打印

给主人留下些什么吧!~~