Linux Sort Doesn't Work with Negative Float Numbers

Linux sort doesn't work with negative float numbers

All those troubles did my local settings. My ubuntu is in Czech:

$ echo $LANG
cs_CZ.UTF-8

In this local setting it's not a decimal point, rather a decimal comma that seperates integer from the rest (as we were thought in math classes, in our language we really do write comma instead of a point).

Therefore:

echo '0,03 >> 0,4 >
> -0,3 >
> 0' | sort -n
> 0
> -0,3 >
> 0,4 >
0,03 >

If you are writing a bash script, set the sorting routine to use the "normal" settings.

export LC_ALL=C

Bash - how to sort negative values?

my problem was that the data wasnt well formatted - because of my locale, I had to sed decimal point into decimal comma - that's how its written in czech republic
thanks

sort numeric column based on absolute values and scientific notation

sed 's/^-\(.*\)/\1-/' | sort -g | sed 's/^\(.*\)-$/-\1/'

The idea is to transform every negative number -123 into 123-, then sort numerically (which ignores the trailing -), then undo the transformation.

How to sort a file, based on its numerical values for a field?

Take a peek at the man page for sort...

   -n, --numeric-sort
compare according to string numerical value

So here is an example...

sort -n filename


Related Topics



Leave a reply



Submit