使用 'sort -g' bash 命令用指数对浮点数进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10311624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:07:47  来源:igfitidea点击:

Sorting floats with exponents with 'sort -g' bash command

bashsortingcommand-line

提问by Alex

I have a file with floats with exponents and I want to sort them. AFAIK 'sort -g' is what I need. But it seems like it sorts floats throwing away all the exponents. So the output looks like this (which is not what I wanted):

我有一个带有指数的浮点数的文件,我想对它们进行排序。AFAIK 'sort -g' 是我需要的。但它似乎对浮点数进行排序,丢弃所有指数。所以输出看起来像这样(这不是我想要的):

$ cat file.txt | sort -g
8.387280091e-05
8.391373668e-05
8.461754562e-07
8.547354437e-05
8.831553093e-06
8.936111118e-05
8.959458896e-07

This brings me to two questions:

这给我带来了两个问题:

  1. Why 'sort -g' doesn't work as I expect it to work?
  2. How cat I sort my file with using bash commands?
  1. 为什么 'sort -g' 不能像我期望的那样工作?
  2. 如何使用 bash 命令对文件进行排序?

回答by Jonatan ?str?m

The problem is that in some countries local settings can mess this up by using ,as the decimal separator instead of .on a system level. Check by typing localein terminal. There should be an entry

问题在于,在某些国家/地区,本地设置可能会将其,用作小数点分隔符而不是.系统级别。通过locale在终端中键入来检查。应该有入口

LC_NUMERIC=en_US.UTF-8

If the value is anything else, change it to the above by editing the locale file

如果该值是其他值,请通过编辑区域设置文件将其更改为上述值

sudo gedit /etc/default/locale

That's it. You can also temporarily use this value by doing

就是这样。您也可以通过执行临时使用此值

LC_ALL=C sort -g file.dat

LC_ALL=Cis shorter to write in terminal, but putting it in the locale file might not be preferable as it could alter some other system-wide behavior such as maybe time format.

LC_ALL=C在终端中写入较短,但将其放入语言环境文件可能并不可取,因为它可能会改变其他一些系统范围的行为,例如时间格式。

回答by David W.

Here's a neat trick:

这是一个巧妙的技巧:

$ sort -te -k2,2n -k1,1n test.txt 
8.461754562e-07
8.959458896e-07
8.831553093e-06
8.387280091e-05
8.391373668e-05
8.547354437e-05
8.936111118e-05

The -tedivides your number into two fields by the ethat separates out the mantissa from the exponent. the -k2,2says to sort by exponent first, then the -k1,1says to sort by your mantissa next.

-te由分你的电话号码分为两个区域e分隔出从指数的尾数。该-k2,2说的指数先进行排序,然后-k1,1说,你的下一个尾数排序。

Works with all versions of the sortcommand.

适用于所有版本的sort命令。

回答by Debaditya

Your method is absolutely correct

你的方法完全正确

cat file.txt | sort -g

If the above code is not working , then try this

如果上面的代码不起作用,那么试试这个

sed 's/\./0000000000000/g' file.txt | sort -g | sed 's/0000000000000/\./g'

Convert '.' to '0000000000000' , sort and again subsitute with '.'. I chose '0000000000000' to replace so as to avoid mismatching of the number with the inputs. You can manipulate the number by your own.

转变 '。' 到 '0000000000000' ,排序并再次替换为 '.'。我选择了 '0000000000000' 来替换,以避免数字与输入不匹配。您可以自己操作数字。