免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2267 | 回复: 7

[文本处理] 自然排序/版本排序 [复制链接]

论坛徽章:
0
发表于 2012-12-14 21:22 |显示全部楼层
本帖最后由 cuteorange 于 2012-12-14 21:24 编辑

* 排序前
#cat data
eth237 GlBlKJAn LXPI
eth191 BYPSdivD NQFL
eth104 smwnzrnX WLsz
eth233 vrMllyud Onbi
eth195 yUJEkrom KvBL
eth90 QEfqcBHD oxYT
eth239 tBJmMpcq clXg
eth178 RuQdYFdG JsWE
eth255 dunTcnWO VrXT
eth232 GVpXbDGT XuwP
* 排序后
eth90 QEfqcBHD oxYT
eth104 smwnzrnX WLsz
eth178 RuQdYFdG JsWE
eth191 BYPSdivD NQFL
eth195 yUJEkrom KvBL
eth232 GVpXbDGT XuwP
eth233 vrMllyud Onbi
eth237 GlBlKJAn LXPI
eth239 tBJmMpcq clXg
eth255 dunTcnWO VrXT

法一:
用GNU sort,
# sort -V data

法二:
sed -r 's/^([a-z]+)([0-9]+)(.*)$/\1 \2\3/' data | sort -n -k2|sed -r 's/([a-z]+) ([0-9]+)(.*)$/\1\2\3/'

法三:
awk '{a[substr($1,4)]=$0}END{n=asorti(a,b,"@ind_num_asc");for(i=1;i<=n;i++)print a[b]}' data

论坛徽章:
0
发表于 2012-12-14 21:37 |显示全部楼层
  1. sort -n -k1.4
复制代码
  1. sort -n -k1.4,1.6
复制代码

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
发表于 2012-12-14 21:48 |显示全部楼层
  1. [root@localhost ~]# sort --version
  2. sort (coreutils) 5.2.1
  3. Written by Mike Haertel and Paul Eggert.

  4. Copyright (C) 2004 Free Software Foundation, Inc.
  5. This is free software; see the source for copying conditions.  There is NO
  6. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  7. [root@localhost ~]# sort -V i
  8. sort: invalid option -- V
  9. Try `sort --help' for more information.
  10. [root@localhost ~]# sed -r 's/^([a-z]+)([0-9]+)(.*)$/\1 \2\3/' i | sort -n -k2|sed -r 's/([a-z]+) ([0-9]+)(.*)$/\1\2\3/'
  11. eth90 QEfqcBHD oxYT
  12. eth104 smwnzrnX WLsz
  13. eth178 RuQdYFdG JsWE
  14. eth191 BYPSdivD NQFL
  15. eth195 yUJEkrom KvBL
  16. eth232 GVpXbDGT XuwP
  17. eth233 vrMllyud Onbi
  18. eth237 GlBlKJAn LXPI
  19. eth239 tBJmMpcq clXg
  20. eth255 dunTcnWO VrXT
  21. [root@localhost ~]# awk '{a[substr($1,4)]=$0}END{n=asorti(a,b,"@ind_num_asc");for(i=1;i<=n;i++)print a[b]}' i
  22. awk: cmd. line:1: fatal: 3 is invalid as number of arguments for asorti
  23. [root@localhost ~]# awk '{a[substr($1,4)]=$0}END{n=asorti(a,b,"@ind_num_asc");for(i=1;i<=n;i++)print a[b[i]]}' i
  24. awk: cmd. line:1: fatal: 3 is invalid as number of arguments for asorti
  25. [root@localhost ~]#
复制代码
第一个用不了,不知道是不是版本问题;第二个可以,但是我觉得太麻烦了,用到了管道,还用到了sort,还不如直接用sort呢;第三个有问题,请你再仔细检查一下。
下面是我的方法:
  1. [root@localhost ~]# sort -n -k1.4 i
  2. eth90 QEfqcBHD oxYT
  3. eth104 smwnzrnX WLsz
  4. eth178 RuQdYFdG JsWE
  5. eth191 BYPSdivD NQFL
  6. eth195 yUJEkrom KvBL
  7. eth232 GVpXbDGT XuwP
  8. eth233 vrMllyud Onbi
  9. eth237 GlBlKJAn LXPI
  10. eth239 tBJmMpcq clXg
  11. eth255 dunTcnWO VrXT
  12. [root@localhost ~]#
复制代码

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
发表于 2012-12-14 21:52 |显示全部楼层
NB,好,学习!

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
发表于 2012-12-14 22:12 |显示全部楼层
回复 4# blackold


    黑哥来个awk的解决方案?

论坛徽章:
0
发表于 2012-12-15 08:46 |显示全部楼层
回复 2# udevu


这个对定长前缀的情况很好,简洁。
赞一个。

论坛徽章:
0
发表于 2012-12-15 08:53 |显示全部楼层
本帖最后由 cuteorange 于 2012-12-15 08:56 编辑

回复 3# yestreenstars


    "第一个用不了,不知道是不是版本问题;第二个可以,但是我觉得太麻烦了,用到了管道,还用到了sort,还不如直接用sort呢;第三个有问题,请你再仔细检查一下。"

Thanks for your concern.

法一:sort版本
#sort --version
sort (GNU coreutils) 8.10
Packaged by Cygwin (8.10-1)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and Paul Eggert.
法二: 可以处理变长前缀的情况,比如
eth0
eth1
fiber3
xxxxssss100
z990
法三:
#awk -V
GNU Awk 4.0.0
Copyright (C) 1989, 1991-2011 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

a[ b [ i ] ]贴进来之后显示变了,呵呵.

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
发表于 2012-12-15 13:05 |显示全部楼层
回复 7# cuteorange


    好吧,看来第一个和第三个都是我的版本问题~第二个没想到还有这种用途~呵呵~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP