排序与指数彩车以“排序-g'bash命令 [英] Sorting floats with exponents with 'sort -g' bash command

查看:175
本文介绍了排序与指数彩车以“排序-g'bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与指数花车的文件,我想对它们进行排序。据我所知排序-g是我需要的。但现在看来似乎排序花车扔掉所有的指数。所以输出看起来像这样(这不是我想要的东西):

  $猫file.txt的|排序-g
8.387280091e-05
8.391373668e-05
8.461754562e-07
8.547354437e-05
8.831553093e-06
8.936111118e-05
8.959458896e-07

这使我想到两个问题:


  1. 为什么'排序-g如我所料不工作它的工作?

  2. 如何猫我我的文件,使用bash命令排序?


解决方案

下面是一个巧妙的方法:

  $排序-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

-te 把你的电话号码到由电子分隔出从指数尾数两个字段。在 -k2,2 说由第一指数进行排序,那么 -k1,1 说你的尾数排序下一个。

作品使用排序命令的所有版本。

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):

$ 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?

解决方案

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 -te divides your number into two fields by the e that separates out the mantissa from the exponent. the -k2,2 says to sort by exponent first, then the -k1,1 says to sort by your mantissa next.

Works with all versions of the sort command.

这篇关于排序与指数彩车以“排序-g'bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆