Unix Sort with Exponential Values?

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.

0

4 Answers

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

1

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.

1

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.