处理前:
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
处理要求:
根据第一列分类,对第二列进行排序,合并连续的。
-
#!/usr/bin/perl
-
my ( %h1, %h2 );
-
map { push @{ $h1{$_->[0]} }, $_->[2]; $h2{$_->[0]} = $_->[1] } sort { $a->[2] <=> $b->[2] } map { [ /(\S+)\s+(\D+)(\d+)/ ] } <DATA>;
-
for my $k ( sort keys %h1 ) {
-
print "$k: ";
-
my $s;
-
for ( 0 .. $#{ $h1{$k} } ) {
-
my $t = "$h2{$k}$h1{$k}[$_]";
-
$s .= $h1{$k}[$_+1] ? $h1{$k}[$_+1] - $h1{$k}[$_] == 1 ? "$t-" : "$t," : $t;
-
}
-
$s =~ s/-[^,]+-\D+/-/;
-
print "$s$/";
-
}
-
__DATA__
-
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
-
#!/bin/awk -f
-
{
-
a=gensub(/([^0-9]+).*/,"\\1",1,$2);
-
b=gensub(/[^0-9]+(.*)/,"\\1",1,$2);
-
c=sprintf("%s%10d",a,b);
-
d[$1"\t"c]=b;
-
}
-
END{
-
for(i=0;i++<asorti(d,e);){
-
split(e[i],f);
-
if(!g)printf "%s: ",f[1];
-
if(g&&g!=f[1]){
-
gsub(/-[^,]+-[^0-9]+/,"-",h);
-
sub(/.$/,"",h);
-
printf "%s\n%s: ",h,f[1];
-
h="";
-
}
-
h=d[e[i+1]]-d[e[i]]==1?h""f[2]d[e[i]]"-":h""f[2]d[e[i]]",";
-
g=f[1];
-
}
-
gsub(/-[^,]+-[^0-9]+/,"-",h);
-
sub(/,[^,]+,$/,"",h);
-
print h;
-
}
阅读(1133) | 评论(0) | 转发(1) |