本文介绍了排序科学和浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在拼命尝试使用命令sort,从科学值和浮点值(正负两个值)中对混合物进行排序,例如:

I have been trying desperately to use the command sort, to sort a mixture out of scientific and floating values which are both positive and negative, e.g.:

-2.0e+00
2.0e+01
2.0e+02
-3.0e-02
3.0e-03
3.0e-02

在没有浮点数或没有科学指数的情况下,它可以正常工作sort -k1 -g file.dat.如前所述,一次使用两者,结果如下:

Without the floating point or without the scientific exponent, it works just fine withsort -k1 -g file.dat. Using both at once as stated before, it results in:

-3.0e-02
-2.0e+00
2.0e+01
2.0e+02
3.0e-02
3.0e-03

这显然是错误的,因为它应该是:

This is obviously wrong since it should be:

-2.0e+00    
-3.0e-02
3.0e-03
3.0e-02
...

有什么想法可以解决这个问题吗?一旦我解决了这个问题,是否有可能对绝对值进行排序(例如,消除负值)?我知道我可以尝试对每个值求平方,排序,取平方根.这样做虽然不太精确,但是有一种很好的,快速而直接的方法会很整洁.

Any idea how I can solve this issue? And once I solve this, is there any possibility to sort the absolute value (e.g. get rid of the negative ones)? I know I could try to square each value, sort, take the square root. Doing this I would be less precise though and it would be neat to have a nice, fast and straightforward way.

我的linux系统:8.12,版权所有©2011

My linux system: 8.12, Copyright © 2011

非常感谢!

更新:如果我在调试模式下运行它sort -k1 -g filename.dat --debug,我将得到以下结果(我将其翻译为英语,输出为德语)

UPDATE: if I run it in the debug mode sort -k1 -g filename.dat --debug I get the following result (I translated it into english, output was german)

 sort: the sorting rules for „de_DE.UTF-8" are used
 sort: key 1 is numerically and involves several fields
-3.0e-02
__
________
-2.0e+00
__
________
2.0e+01
_
_______
2.0e+02
_
_______
3.0e-02
_
_______
3.0e-03
_
_______

推荐答案

基于问题下的注释,这是一个语言环境问题:sort使用的是语言环境,期望将,作为小数点分隔符,而您的文本有..理想的解决方案是使sort使用不同的语言环境,并希望有人会写出正确的答案.

Based on comments under the question, this is a locale issue: sort is using a locale, which expects , as decimal separator, while your text has .. Ideal solution would to make sort use a different locale, and hopefully someone will write a correct answer covering that.

但是,如果您不能或不想更改sort的工作方式,则可以更改其获得的输入.通过使sort从管道中获取其输入并在途中对其进行修改,这是最简单的.在这里,将每个.更改为,就足够了,因此选择的工具是 c11> :

But, if you can't, or don't want to, change how sort works, then you can change the input it gets. This is easiest by making sort take its input from pipe, and modify it on the way. Here it is enough to change every . to ,, so the tool of choice is tr:

cat file.dat | tr . , | sort -k1 -g 

此解决方案有一个很大的缺点:如果使用带.作为小数点分隔符的语言环境执行命令,则不进行固定操作,这会破坏排序.因此,如果您正在编写可以在其他地方使用的shell脚本,请不要这样做.

This solution has one big drawback: if command is executed with locale where sort uses . as decimal separator, then instead of fixing, this will break the sorting. So if you are writing a shell script, which may be used elsewhere, don't do this.

重要说明:上面的命令不必要地使用了 cat .每个想要将自己当作专业的shell脚本程序员来认真对待的人,不要那么做!

Important note: Above command has unnecessary use of cat. Everybody who wants themselves to be taken seriously as professional shell script programmers, don't do that!

这篇关于排序科学和浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 22:26