- 论坛徽章:
- 1
|
顺序有问题,没办法了用asort排序后就这样,不过添加了去重的功能[code][root@localhost ~]# cat 1.txt
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
[root@localhost ~]# awk '{
a[NR]=$1FS$2
}
END{
asort(a)
for (i=1;i<=NR;i++) {
split(a[i],b,FS)
if (!c[b[1]])
c[b[1]]=b[2]","
else {
count=split(c[b[1]],d,",")
match(d[count-1],/a-z+/)
old1=substr(d[count-1],RSTART,RLENGTH)
match(b[2],/a-z+/)
new1=substr(b[2],RSTART,RLENGTH)
match(d[count-1],/[0-9]+$/)
old2=substr(d[count-1],RSTART,RLENGTH)
match(b[2],/[0-9]+/)
new2=substr(b[2],RSTART,RLENGTH)
if (old1==new1 && (old2+1)==new2||old2==new2) {
c[b[1]]=substr(c[b[1]],1,length(c[b[1]])-1)
sub(/-[0-9]+$/,"",c[b[1]])
c[b[1]]=c[b[1]]"-"new2","
}
else
c[b[1]]=c[b[1]]""b[2]","
}
}
for (i in c)
print i":"substr(c[i],1,length(c[i])-1)
}' 1.txt
apple:cat106-109,cat12,cat123,cat125
pear:dog101,dog103-105,dog11,mouse106-109,mouse123,mouse125
[root@localhost ~]# [/code] |
|