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.
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.
-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 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With