用unix排序对科学数进行排序
我试图用unix排序对这些数字进行排序,但似乎不起作用:
2e-13
1e-91
2e-13
1e-104
3e-19
9e-99
这是我的命令:
sort -nr file.txt
正确的方法是什么?
I tried to sort these number with Unix sort, but it doesn't seem to work:
2e-13
1e-91
2e-13
1e-104
3e-19
9e-99
This is my command:
sort -nr file.txt
What's the right way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
-g
(长表单- 一般数字
),而不是-n
。-G
选项通过strtod
传递数字以获取其值,并将识别此格式。我不确定这是否可用于
sort
的所有实现,还是GNU。Use
-g
(long form--general-numeric-sort
) instead of-n
. The-g
option passes the numbers throughstrtod
to obtain their value, and it will recognize this format.I'm not sure if this is available on all implementations of
sort
, or just the GNU one.如果您的
sort
没有-g
,则是另一种方式。if your
sort
doesn't have-g
, another way.只需做两件事:
-g
(或- 通用数字
)即可正确处理 sort 正确处理exp-numbers。lc_all = C
。如果您的数据可能包含除ASCII以外的某些语言特定符号,则Sort
对于语言环境非常明智。因此,使用lc_all = c
是的通用方法,您使用sort
,它使sort
处理来处理输入流是二进制的,您不会遇到任何问题。因此,通用解决方案是:
cat file.txt | lc_all = c sort -gr |更少的
也是我在
.bashrc
文件中分类的别名:别名csort =“ lc_all = c sort”
,以备多种令人信服的使用。
Just do two things:
-g
(or--general-numeric-sort
) to makesort
treat Exp-numbers correctly.LC_ALL=C
. Thesort
is very sensible to locale if your data may contain some language-specific symbols except ASCII. So usingLC_ALL=C
is the universal approach for every case you use thesort
, it makessort
to treat the input stream as binary, and you wouldn't have any problems.So the universal solution is:
cat file.txt | LC_ALL=C sort -gr | less
Also I made an alias for sort in my
.bashrc
file:alias csort="LC_ALL=C sort"
for much convinient use.
好的,这是Python脚本的未完全测试的版本。假设的用法:
不幸的是,我在Windows上开发了它,并且使用了2个不同版本的Python安装,我无法正确测试它。警告:需要最新的Python(添加或更改打印功能)。注意:Back_to_file标志可以是一个可选参数,尽管使用UNIX,您始终可以重定向...即使在Windows中也可以。
Ok, here is a not fully tested version of Python script. Supposed usage:
Unfortunately I developed this on Windows, and with 2 different versions of Python installed I cannot test it properly. Warning: requires newest Python (with, print functions added or changed). Note: back_to_file flag can be an optional parameter, although with Unix you can always redirect ... even in Windows you can.