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

全部博文(54)

文章存档

2014年(37)

2013年(17)

分类: LINUX

2014-01-15 17:09:33

处理前:
apple   cat108
pear    mouse123
apple   cat107
apple   cat123
apple   cat12
pear    mouse108
apple   cat109
pear    mouse106
apple   cat106
pear    mouse125
apple   cat125
pear    mouse107
pear    mouse109

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

处理要求:
根据第一列分类,对第二列进行排序,合并连续的。


点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. my ( %h1, %h2 );
  3. map { push @{ $h1{$_->[0]} }, $_->[2]; $h2{$_->[0]} = $_->[1] } sort { $a->[2] <=> $b->[2] } map { [ /(\S+)\s+(\D+)(\d+)/ ] } <DATA>;
  4. for my $k ( sort keys %h1 ) {
  5.         print "$k: ";
  6.         my $s;
  7.         for ( 0 .. $#{ $h1{$k} } ) {
  8.                 my $t = "$h2{$k}$h1{$k}[$_]";
  9.                 $s .= $h1{$k}[$_+1] ? $h1{$k}[$_+1] - $h1{$k}[$_] == 1 ? "$t-" : "$t," : $t;
  10.         }
  11.         $s =~ s/-[^,]+-\D+/-/;
  12.         print "$s$/";
  13. }
  14. __DATA__
  15. apple cat108
  16. pear mouse123
  17. apple cat107
  18. apple cat123
  19. apple cat12
  20. pear mouse108
  21. apple cat109
  22. pear mouse106
  23. apple cat106
  24. pear mouse125
  25. apple cat125
  26. pear mouse107
  27. pear mouse109


点击(此处)折叠或打开

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


阅读(1133) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~