Chinaunix

标题: 小数点后2位数排序问题请教! [打印本页]

作者: cnbugs    时间: 2010-11-05 18:55
标题: 小数点后2位数排序问题请教!
本帖最后由 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

如何来处理这种情况?
作者: ghp268    时间: 2010-11-05 19:01
本帖最后由 ghp268 于 2010-11-05 19:05 编辑
  1. sort  -k1.1 urfile
复制代码
=====看错了==编辑下
  1. sort -t. -k2n file
复制代码

作者: ghp268    时间: 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.
复制代码

作者: cnbugs    时间: 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
作者: ywlscpl    时间: 2010-11-05 20:11
回复 4# cnbugs


    sort -t. -k1,1n -k2,2n file
作者: ghp268    时间: 2010-11-05 20:31
回复 4# cnbugs


    -k1.3 指的是第一个域的第三个字符开始比较
    n指按数字值排序,即-n
   英文资料要多看,man
作者: cnbugs    时间: 2010-11-05 20:48
6楼的说明 加5楼的代码理解了参数的用法。
处理文件成功,感谢!
作者: dibug    时间: 2010-11-05 20:58
看置顶十二篇之 sort




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2