46

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.

drbunsen
  • 10,139
  • 21
  • 66
  • 94

4 Answers4

75

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

Finslicer
  • 868
  • 8
  • 8
  • 1
    Thanks, I read the man page, but I didn't see anything about the -g flag. This did precisely what I was looking for. – drbunsen Sep 14 '11 at 14:53
10

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.

pearcoding
  • 1,149
  • 1
  • 9
  • 28
Pavel Kurochkin
  • 101
  • 1
  • 2
  • 1
    I had this problem as well. Here I found a solution https://askubuntu.com/questions/724338/how-to-set-lc-numeric-to-english-permanently – psmith Jun 05 '17 at 19:35
3

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

amc
  • 813
  • 1
  • 15
  • 28
  • Could you elaborate? – Kmeixner Apr 04 '16 at 17:41
  • how so? -g is for 'use generic numerical value'. Please read the above post about why to use -g. If you still have questions I will definitely try to answer them! – amc Apr 04 '16 at 17:51
0
  1. You should use -g
  2. Don't use -n

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

Shicheng Guo
  • 1,233
  • 16
  • 19