开发者

UNIX sort with exponential values?

I have a csv file with 7 fields of data. I want to sort the 7th field in reverse numerial order (smallest values first). The 7th field of data looks like this:

0.498469643137
1
6.98112003175e-10
9.11278069581e-06

I have tried to use the UNIX sort tool like this:

$ sort -t"," -n -k -r 7 <my_file>

The problem I am having is that sort does not recognize exponential form. For example, 开发者_运维百科sort thinks 6.98112003175e-10 is larger than 1. How can I use sort to sort a csv column, but recognize the scientific notation? Thanks in advance for the help.


sort with '-g' option should do the trick for you. -g option indicates 'use generic numerical value' for sorting


Please note, that your locale may assume another delimiter: For example, in russian localization ',' character delimits parts of number rather than '.'. In this case you should take into account the LANG variable.

In my case LANG was set to ru_RU.KOI8-R and so sort -g gave me wrong result.


So - just to give an example for those who do not know how to use it: instead of "-n" you use "-g". l = 1,0.3,6.01e-10

sort -t$',' -n example.txt

0.3 1 6.01e-10

sort -t$',' -g example.txt

6.01e-10 0.3 1


  1. You should use -g
  2. Don't use -n

The correct command is sort -g -k7,7 input.txt

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜