Sorting scientific and floating

I was desperate to use the sort command to sort a mixture of scientific and floating values ​​that are both positive and negative, for example:

 -2.0e+00 2.0e+01 2.0e+02 -3.0e-02 3.0e-03 3.0e-02 

Without floating point or without scientific exponent, it works great with sort -k1 -g file.dat . Using both at once, as indicated above, this results in:

 -3.0e-02 -2.0e+00 2.0e+01 2.0e+02 3.0e-02 3.0e-03 

This is clearly wrong, as it should be:

 -2.0e+00 -3.0e-02 3.0e-03 3.0e-02 ... 

Any idea how I can solve this problem? And as soon as I solve this, is it possible to sort the absolute value (for example, get rid of the negative ones)? I know that I could try to square each value, sort, take the square root. By doing this, I would be less accurate, and it would be neat to have a nice, fast, and easy way.

My Linux system: 8.12, Copyright © 2011

Many thanks!

UPDATE: if I ran it in debug mode sort -k1 -g filename.dat --debug , I get the following result (I translated it into English, the output was German)

  sort: the sorting rules for „de_DE.UTF-8" are used sort: key 1 is numerically and involves several fields -3.0e-02 __ ________ -2.0e+00 __ ________ 2.0e+01 _ _______ 2.0e+02 _ _______ 3.0e-02 _ _______ 3.0e-03 _ _______ 
+5
source share
1 answer

Based on the comments on this, this is a locale problem: sort uses a locale that expects as a decimal separator, and your text has . . An ideal solution would be to make sort use a different language, and hopefully someone will write the correct answer covering this.

But if you cannot or do not want to change the way sort works, you can change the input that he entered. This is easiest to do if sort take your input from the pipe and change it along the way. It’s enough to change everyone here . on tr so the tr selection tool:

 cat file.dat | tr . , | sort -k1 -g 

This solution has one big drawback: if the command is executed with the locale where sort uses . As a decimal separator, instead of fixing it will lead to breakage of the sort. Therefore, if you are writing a shell script that can be used elsewhere, do not do this.

Important Note: The Above command has unnecessary use of cat . Anyone who wants to be taken seriously as professional script programmers, don't do this!

+1
source

Source: https://habr.com/ru/post/1204558/


All Articles