Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 619
drbunsen Avatar asked Sep 14 '11 13:09

drbunsen


People also ask

How do I sort in ascending order in Unix?

Option -n In Unix, when you try to sort a file in a numeric way, you can use the option '-n' with the sort command. This command is used to sort the numeric contents present in the file. Be default, it sorts in ascending order.

How do you sort a value in Unix?

-k Option: Unix provides the feature of sorting a table on the basis of any column number by using -k option. Use the -k option to sort on a certain column. For example, use “-k 2” to sort on the second column.

How do you sort numerically in Linux?

How to sort by number. To sort by number pass the -n option to sort . This will sort from lowest number to highest number and write the result to standard output. Suppose a file exists with a list of items of clothing that has a number at the start of the line and needs to be sorted numerically.

How do I sort alphabetically in Unix?

The sort command sorts the contents of a file, in numeric or alphabetic order, and prints the results to standard output (usually the terminal screen). The original file is unaffected. The output of the sort command will then be stored in a file named newfilename in the current directory.


2 Answers

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

like image 119
Finslicer Avatar answered Sep 18 '22 16:09

Finslicer


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.

like image 39
Pavel Kurochkin Avatar answered Sep 20 '22 16:09

Pavel Kurochkin