免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9333 | 回复: 17
打印 上一主题 下一主题

perl 使用sort怎么实现多字段的数字排序 [复制链接]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-06-23 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-01-09 13:39 |只看该作者 |倒序浏览
有一个文件mtk.service.test1.info.log,内容如下:
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 283)
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 29
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 367)
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 419)
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 429)
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 436)
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 441)
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 459)
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 481)
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 485)
[cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 486)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 170)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 181)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 21
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 223)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 250)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 282)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 283)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 286)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 296)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 29
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 309)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 335)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 337)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 367)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 371)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 376)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 395)
[cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 40



我想让它按照数字排序。(shell使用:sort -k 4 mtk.service.test1.info.log),请牛人指点!

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34子鼠
日期:2013-12-14 14:57:19射手座
日期:2014-04-25 21:23:23
2 [报告]
发表于 2013-01-09 14:00 |只看该作者
  1. map{print $_->[0]}sort{$a->[1] <=> $b->[1]}map{[$_,(split(/(\d+)/))[1]]}<DATA>;
  2. __DATA__
  3. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 283)
  4. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 29
  5. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 367)
  6. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 419)
  7. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 429)
  8. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 436)
  9. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 441)
  10. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 459)
  11. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 481)
  12. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 485)
  13. [cn.emagsoftware.mtk.client.BillingInterfaceClient] (WebContainer : 486)
  14. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 170)
  15. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 181)
  16. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 21
  17. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 223)
  18. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 250)
  19. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 282)
  20. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 283)
  21. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 286)
  22. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 296)
  23. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 29
  24. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 309)
  25. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 335)
  26. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 337)
  27. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 367)
  28. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 371)
  29. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 376)
  30. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 395)
  31. [cn.emagsoftware.mtk.client.RegisterUcidClient] (WebContainer : 40
复制代码

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-06-23 06:20:00
3 [报告]
发表于 2013-01-09 15:05 |只看该作者
高人啊,我关注你很久了!!!! 回复 2# mcshell


   

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-06-23 06:20:00
4 [报告]
发表于 2013-01-09 15:06 |只看该作者
能讲解一下吗,大神回复 2# mcshell


   

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34子鼠
日期:2013-12-14 14:57:19射手座
日期:2014-04-25 21:23:23
5 [报告]
发表于 2013-01-09 15:15 |只看该作者
本帖最后由 mcshell 于 2013-01-09 15:16 编辑

回复 4# jiang870320


    别。。我也是跟着版主和一些低调的人偷着学习的。
split(/(\d+)/)
会以数字分割,注意这里是有括号的。表示匹配的自身也捕获。
然后放在一个匿名数组里,然后用sort进行排序。
这种排序方法是一个施瓦茨转化。。具体看仙子的帖子:
http://bbs.chinaunix.net/thread-3768308-1-1.html

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-06-23 06:20:00
6 [报告]
发表于 2013-01-09 15:16 |只看该作者
看懂了!!!!!!谢谢

论坛徽章:
8
双子座
日期:2013-08-31 07:37:12金牛座
日期:2013-09-09 18:49:12处女座
日期:2013-09-23 11:43:14处女座
日期:2013-10-09 19:48:21狮子座
日期:2014-03-24 18:22:12丑牛
日期:2014-04-22 22:07:51申猴
日期:2014-06-12 21:54:13双鱼座
日期:2014-06-13 21:52:31
7 [报告]
发表于 2013-01-09 15:16 |只看该作者
我猜这个是你想要的
  1. perl -e 'print sort{ (split(/:/,$a))[1] <=> (split(/:/,$b))[1] } <>' filename
复制代码
回复 1# jiang870320


   

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-06-23 06:20:00
8 [报告]
发表于 2013-01-09 16:09 |只看该作者
施瓦茨排序很好用啊!

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-06-23 06:20:00
9 [报告]
发表于 2013-01-09 16:10 |只看该作者
谢谢,豁然开朗啊回复 7# kernel69


   

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34狮子座
日期:2014-03-27 15:44:382015年辞旧岁徽章
日期:2015-03-03 16:54:15
10 [报告]
发表于 2013-01-09 16:39 |只看该作者
我以前也想过这个问题, 学习了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP