免费注册 查看新帖 |

Chinaunix

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

sort排序-科学表达式无法正确排序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-21 12:56 |只看该作者 |倒序浏览
大家好。
我想请问大家在用shell的sort命令进行排序的时候,用-n选项表示是对数值排序。但是对于以科学技术表示的无法排序,该如何处理?谢谢。
示例如下:
-----------------------------------------test----------------------------
郝蕾        0.006323855243722304        4.0        8.0        137.0
郭亮        4.615952732644018E-5        1.0        2.0        1.0
郭伯雄        1.8463810930576072E-4        1.0        1.0        2.0
---------------------------------------------------------------------
我对以上用命令:sort -n --k=2,2 test -r -o rsult,进行排序,得到的结果如下:

-----------------------------------------result----------------------------
郭亮        4.615952732644018E-5        1.0        2.0        1.0
郭伯雄        1.8463810930576072E-4        1.0        1.0        2.0
郝蕾        0.006323855243722304        4.0        8.0        137.0
---------------------------------------------------------------------
正确结果该为:
-----------------------------------------right----------------------------
郝蕾        0.006323855243722304        4.0        8.0        137.0
郭伯雄        1.8463810930576072E-4        1.0        1.0        2.0
郭亮        4.615952732644018E-5        1.0        2.0        1.0
---------------------------------------------------------------------

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2012-04-21 13:28 |只看该作者
试试-g,如果版本支持的话

论坛徽章:
0
3 [报告]
发表于 2012-04-21 13:34 |只看该作者
回复 2# waker


    正确了。谢谢。
    man 说的-g表示general-numeric-sort,这是什么意思,包括一些别的情况么?谢谢。

论坛徽章:
0
4 [报告]
发表于 2012-04-21 15:14 |只看该作者
Zigengdi 发表于 2012-04-21 13:34
回复 2# waker


字面理解:视为一般数字

不知是否正确

论坛徽章:
0
5 [报告]
发表于 2012-04-21 20:14 |只看该作者
-n 是数值字符排序
-g 是根据数值的大小排序
说错了 请轻拍 呵呵

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
6 [报告]
发表于 2012-04-21 20:17 |只看该作者
回复 5# g_programming


    数值字符排序排序是什么意思,给个例子吧。

论坛徽章:
0
7 [报告]
发表于 2012-04-21 21:27 |只看该作者
回复 6# Shell_HAT


    我是按照手册上面翻译的 呵呵 数值字符排序就是指的是0-9这个10个字符
(1)按照数值字符排序
郝蕾        0.006323855243722304        
郭亮        4.615952732644018E-5      
郭伯雄      1.8463810930576072E-4      
第二列的数值字符为0,4,1,所以排序出来为   
郭亮        4.615952732644018E-5      
郭伯雄      1.8463810930576072E-4
郝蕾        0.006323855243722304   
(2)按照数值的大小排序 是指的是字符串转换为数值的大小
所以为
郝蕾        0.006323855243722304     
郭伯雄      1.8463810930576072E-4   
郭亮        4.615952732644018E-5      

不知道说对没?

论坛徽章:
0
8 [报告]
发表于 2012-04-22 22:27 |只看该作者
回复 5# g_programming

牛人啊  拜大神
   

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
9 [报告]
发表于 2012-04-23 08:53 |只看该作者
本帖最后由 waker 于 2012-04-23 08:54 编辑

info sort 有详解

  1. `-g'
  2. `--general-numeric-sort'
  3. `--sort=general-numeric'
  4.      Sort numerically, using the standard C function `strtod' to convert
  5.      a prefix of each line to a double-precision floating point number.
  6.      This allows floating point numbers to be specified in scientific
  7.      notation, like `1.0e-34' and `10e100'.  The `LC_NUMERIC' locale
  8.      determines the decimal-point character.  Do not report overflow,
  9.      underflow, or conversion errors.  Use the following collating
  10.      sequence:

  11.         * Lines that do not start with numbers (all considered to be
  12.           equal).

  13.         * NaNs ("Not a Number" values, in IEEE floating point
  14.           arithmetic) in a consistent but machine-dependent order.

  15.         * Minus infinity.

  16.         * Finite numbers in ascending numeric order (with -0 and +0
  17.           equal).


  18.         * Plus infinity.

  19.      Use this option only if there is no alternative; it is much slower
  20.      than `--numeric-sort' (`-n') and it can lose information when
  21.      converting to floating point.


复制代码

  1. `-n'
  2. `--numeric-sort'
  3. `--sort=numeric'
  4.      Sort numerically.  The number begins each line and consists of
  5.      optional blanks, an optional `-' sign, and zero or more digits
  6.      possibly separated by thousands separators, optionally followed by
  7.      a decimal-point character and zero or more digits.  An empty
  8.      number is treated as `0'.  The `LC_NUMERIC' locale specifies the
  9.      decimal-point character and thousands separator.  By default a
  10.      blank is a space or a tab, but the `LC_CTYPE' locale can change
  11.      this.

  12.      Comparison is exact; there is no rounding error.

  13.      Neither a leading `+' nor exponential notation is recognized.  To
  14.      compare such strings numerically, use the `--general-numeric-sort'
  15.      (`-g') option.

复制代码

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
10 [报告]
发表于 2012-04-23 08:57 |只看该作者
关于6#的提法俺的理解:
如果使用-n选项
sort认为1.8463810930576072E-4根本就不是一个合法数值,只能按缺省的字串排序方法排序
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP