原帖:处理前:
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)进行排序。
-
#!/bin/awk -f
-
{
-
m=gensub(/([^0-9]+).*/,"\\1",1,$2)
-
n=gensub(/[^0-9]+(.*)/,"\\1",1,$2)
-
t=$1" "sprintf("%s%10s",m,n)
-
a[t]=$1
-
b[t]=m
-
c[t]=n
-
}
-
END{
-
for(i=0;i++<asorti(c,d);){
-
if(!k)printf "%s: ",a[d[i]]
-
if(k&&k!=a[d[i]]){
-
gsub(/-[^,]+-[^0-9]+/,"-",s)
-
printf "%s\n%s: ",s,a[d[i]]
-
s=""
-
}
-
k=a[d[i]]
-
if(s){
-
if(a[d[i]]==a[d[i-1]]){
-
if(b[d[i]]!=b[d[i-1]]){s=s","b[d[i]]c[d[i]];continue}
-
s=c[d[i]]-c[d[i-1]]==1?s"-"b[d[i]]c[d[i]]:s","b[d[i]]c[d[i]]
-
}
-
} else s=b[d[i]]c[d[i]]
-
}
-
gsub(/-[^,]+-[^0-9]+/,"-",s)
-
print s;
-
}
-
#!/bin/awk -f
-
{
-
match($2,/([^0-9]+)(.*)/,a)
-
b[$1][a[1]][a[2]]=a[2]
-
}
-
END{
-
for(i in b){
-
printf i":\t"
-
for(j=0;j++<asorti(b[i],c);){
-
for(k=0;k++<asort(b[i][c[j]],d);){
-
t=t?d[k]-d[k-1]==1?t"-"c[j]d[k]:t","c[j]d[k]:c[j]d[k]
-
}
-
s=s?s","t:t
-
t=""
-
}
-
gsub(/-[^,]+-[^0-9]+/,"-",s)
-
print s
-
s=""
-
}
-
}
-
#!/usr/bin/perl
-
my %h;
-
map { push @{ $h{$_->[0]}{$_->[1]} }, $_->[2] } sort { $a->[2] <=> $b->[2] } map { [ /(\S+)\s+(\D+)(\d+)/ ] } <DATA>;
-
for my $k1 ( sort keys %h ) {
-
print "$k1: ";
-
my $s1;
-
for my $k2 ( sort keys %{ $h{$k1} } ) {
-
my $s2;
-
for ( 0 .. $#{ $h{$k1}{$k2} } ) {
-
my $t = "$k2$h{$k1}{$k2}[$_]";
-
$s2 .= $h{$k1}{$k2}[$_+1] ? $h{$k1}{$k2}[$_+1] - $h{$k1}{$k2}[$_] == 1 ? "$t-" : "$t," : $t;
-
}
-
$s1 .= $s1 ? ",$s2" : $s2;
-
}
-
$s1 =~ s/-[^,]+-\D+/-/g;
-
print "$s1$/";
-
}
-
__DATA__
-
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
阅读(1179) | 评论(0) | 转发(1) |