简体   繁体   English

排序科学和浮动

[英]Sort scientific and float

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, eg: 我一直在拼命地使用命令sort ,从科学和浮动值中排除混合物,这些值既有正面也有负面,例如:

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

Without the floating point or without the scientific exponent, it works just fine with sort -k1 -g file.dat . 没有浮点或没有科学指数,它可以正常使用sort -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 (eg 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. 这样做我会不那么精确,但是有一个漂亮,快速和直接的方式是很好的。

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

Thank you very much! 非常感谢你!

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 -k1 -g filename.dat --debug我得到以下结果(我把它翻译成英文,输出是德文)

 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
_
_______

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 . 基于这个问题下评论,这是一个语言环境的问题: sort使用的语言环境,它预计,作为小数点分隔符,而你的文本有. . Ideal solution would to make sort use a different locale, and hopefully someone will write a correct answer covering that. 理想的解决方案是使sort使用不同的区域设置,并希望有人会写出正确的答案。

But, if you can't, or don't want to, change how sort works, then you can change the input it gets. 但是,如果您不能或不想更改sort工作方式,那么您可以更改它所获得的输入。 This is easiest by making sort take its input from pipe, and modify it on the way. 这是最简单的通过使sort采取从管道的输入,并对其进行修改的方式。 Here it is enough to change every . 在这里改变每一个就足够了. to , , so the tool of choice is tr : ,所以选择的工具是tr

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

This solution has one big drawback: if command is executed with locale where sort uses . 这个解决方案有一个很大的缺点:if命令是使用sort使用的locale执行的. 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. 因此,如果您正在编写可能在其他地方使用的shell脚本,请不要这样做。

Important note: Above command has unnecessary use of cat . 重要提示:上述命令不必使用cat Everybody who wants themselves to be taken seriously as professional shell script programmers, don't do that! 每个想要成为专业shell脚本程序员的人都不要那么做!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM