免费注册 查看新帖 |

Chinaunix

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

小数点后2位数排序问题请教! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-05 18:55 |只看该作者 |倒序浏览
本帖最后由 cnbugs 于 2010-11-05 19:35 编辑

文件t.txt:
1.1
1.30
1.2
1.31
1.11
3.3
2.0
1.3
1.10

想要的结果是:
1.1
1.2
1.3
1.10
1.11
1.30
1.31
2.0
3.3

用 sort t.txt -n 结果如下:
1.1
1.10
1.11
1.2
1.3
1.30
1.31
2.0
3.3

如何来处理这种情况?

论坛徽章:
0
2 [报告]
发表于 2010-11-05 19:01 |只看该作者
本帖最后由 ghp268 于 2010-11-05 19:05 编辑
  1. sort  -k1.1 urfile
复制代码
=====看错了==编辑下
  1. sort -t. -k2n file
复制代码

论坛徽章:
0
3 [报告]
发表于 2010-11-05 19:08 |只看该作者
or
  1. sort  -k1.3n file
复制代码
  1. info sort

  2. `-k POS1[,POS2]'
  3. `--key=POS1[,POS2]'
  4.      Specify a sort field that consists of the part of the line between
  5.      POS1 and POS2 (or the end of the line, if POS2 is omitted),
  6.      _inclusive_.

  7.      Each POS has the form `F[.C][OPTS]', where F is the number of the
  8.      field to use, and C is the number of the first character from the
  9.      beginning of the field.  Fields and character positions are
  10.      numbered starting with 1; a character position of zero in POS2
  11.      indicates the field's last character.  If `.C' is omitted from
  12.      POS1, it defaults to 1 (the beginning of the field); if omitted
  13.      from POS2, it defaults to 0 (the end of the field).  OPTS are
  14.      ordering options, allowing individual keys to be sorted according
  15.      to different rules; see below for details.  Keys can span multiple
  16.      fields.
复制代码

论坛徽章:
0
4 [报告]
发表于 2010-11-05 19:25 |只看该作者
本帖最后由 cnbugs 于 2010-11-05 19:38 编辑

E文资料看不懂,对参数k的理解不知道对不对:
sort  -k1.3n file

1是第1个字段
.3是1-3字符
n等同于 -n ?


谢谢你的代码,文件内容我没贴全,我把文件内容变了一下结果又错了,如下:
2.0
1.1
1.10
1.11
1.2
1.3
3.3
1.30
1.31

论坛徽章:
0
5 [报告]
发表于 2010-11-05 20:11 |只看该作者
回复 4# cnbugs


    sort -t. -k1,1n -k2,2n file

论坛徽章:
0
6 [报告]
发表于 2010-11-05 20:31 |只看该作者
回复 4# cnbugs


    -k1.3 指的是第一个域的第三个字符开始比较
    n指按数字值排序,即-n
   英文资料要多看,man

论坛徽章:
0
7 [报告]
发表于 2010-11-05 20:48 |只看该作者
6楼的说明 加5楼的代码理解了参数的用法。
处理文件成功,感谢!

论坛徽章:
0
8 [报告]
发表于 2010-11-05 20:58 |只看该作者
看置顶十二篇之 sort
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP