用unix排序对科学数进行排序

发布于 2025-02-08 20:55:56 字数 200 浏览 1 评论 0原文

我试图用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

油饼 2025-02-15 20:55:56

使用-g(长表单- 一般数字),而不是-n-G选项通过strtod传递数字以获取其值,并将识别此格式。

我不确定这是否可用于sort的所有实现,还是GNU。

Use -g (long form --general-numeric-sort) instead of -n. The -g option passes the numbers through strtod 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.

咿呀咿呀哟 2025-02-15 20:55:56

如果您的sort没有-g,则是另一种方式。

$ printf "%.200f\n" $(<file) |sort -n |xargs printf "%g\n"
1e-104
9e-99
1e-91
3e-19
2e-13
2e-13

$ sort -g file
1e-104
9e-99
1e-91
3e-19
2e-13
2e-13

$ printf "%.200f\n" `cat file` |sort -n |xargs printf "%g\n"

if your sort doesn't have -g, another way.

$ printf "%.200f\n" $(<file) |sort -n |xargs printf "%g\n"
1e-104
9e-99
1e-91
3e-19
2e-13
2e-13

$ sort -g file
1e-104
9e-99
1e-91
3e-19
2e-13
2e-13

$ printf "%.200f\n" `cat file` |sort -n |xargs printf "%g\n"
囚我心虐我身 2025-02-15 20:55:56

只需做两件事:

  1. 使用-g(或- 通用数字)即可正确处理 sort 正确处理exp-numbers。
  2. 使用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:

  1. Use -g (or --general-numeric-sort) to make sort treat Exp-numbers correctly.
  2. Use LC_ALL=C. The sort is very sensible to locale if your data may contain some language-specific symbols except ASCII. So using LC_ALL=C is the universal approach for every case you use the sort, it makes sort 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.

黄昏下泛黄的笔记 2025-02-15 20:55:56

好的,这是Python脚本的未完全测试的版本。假设的用法:

sort_script.py file.txt

不幸的是,我在Windows上开发了它,并且使用了2个不同版本的Python安装,我无法正确测试它。警告:需要最新的Python(添加或更改打印功能)。注意:Back_to_file标志可以是一个可选参数,尽管使用UNIX,您始终可以重定向...即使在Windows中也可以。

#!/usr/bin/env python3.1
# Note: requires newer python

import sys

#Remove this line:
sys.argv = ('', 'file.txt')

assert(len(sys.argv) == 2)

with open(sys.argv[1], 'r') as fin:
    lines = fin.readlines()

lines_sorted = sorted(lines, key=lambda x: float(x))

back_to_file = False # Change this if needed

if back_to_file:
    with open(sys.argv[1], 'w') as fout:
        fout.writelines(lines_sorted)
else:
    for lns in lines_sorted:
        print(lns, end='') # Suppress new line

Ok, here is a not fully tested version of Python script. Supposed usage:

sort_script.py file.txt

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.

#!/usr/bin/env python3.1
# Note: requires newer python

import sys

#Remove this line:
sys.argv = ('', 'file.txt')

assert(len(sys.argv) == 2)

with open(sys.argv[1], 'r') as fin:
    lines = fin.readlines()

lines_sorted = sorted(lines, key=lambda x: float(x))

back_to_file = False # Change this if needed

if back_to_file:
    with open(sys.argv[1], 'w') as fout:
        fout.writelines(lines_sorted)
else:
    for lns in lines_sorted:
        print(lns, end='') # Suppress new line
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文