优秀是一种习惯
分类: Python/Ruby
2013-02-25 18:20:02
有这样一段文本:
aaa bbb abc bbb abc abb aab acc abb bbb abc abb aab acc aab acc abb bcc bbc...
需要按照bbb、abc、abb、acc、aaa顺序排序,如果数据不是这其中之一,则排在最后面,这个顺序没有更多要求。
代码如下:
#!/usr/bin/awk -f
func shuchu(n,item){
for (x=1;x<=n;x++)
print item" "
}
BEGIN{
key[1] = "bbb"
key[2] = "abc"
key[3] = "abb"
key[4] = "acc"
key[5] = "aaa"
RS=" "
ORS=""
}
{
gsub("\n","",$0)
num[$0]++
}
END{
for(i=1;i<=6;i++){
shuchu(num[key[i]],key[i])
delete num[key[i]]
}
for(i in num)
shuchu(num[i],i)
printf "\n" }
执行结果: