处理前:
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
pear: mouse106-109,mouse123,mouse125
-
#!/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]f[2]){
-
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]f[2];
-
}
-
gsub(/-[^,]+-[^0-9]+/,"-",h);
-
sub(/,[^,]+,$/,"",h);
-
print h;
-
}
阅读(1126) | 评论(0) | 转发(1) |