Sorting scientific numbers with sort

Hey everybody,

I'm trying to sort scientific numbers in a descending order using the command

sort -gr <file>

.

It works fine on a Linux-Server, but doesn't on my macbook pro with OS X 10.10.3 (Yosemite).
I tried to sort the following:

6.38e-10
6.38e-10
1.80e-11
1.00e-10
1.48e-12

And got this:

6.38e-10
6.38e-10
1.80e-11
1.48e-12
1.00e-10

Instead of this:

6.38e-10
6.38e-10
1.00e-10
1.80e-11
1.48e-12

Does anyone have an idea why mac is not recognizing the scientific notation despite the -g option?

Many thanks!

Heyas, welcome to the forum.

For future posts/threads, please use code tags, for this list its not that much required though.
But this is beeing an expection.

Replace g by n and remove the r (or not).

hth

EDIT:

$ sort -nr sample.txt 
6.38e-10
6.38e-10
1.80e-11
1.48e-12
1.00e-10

$ sort -n sample.txt 
1.00e-10
1.48e-12
1.80e-11
6.38e-10
6.38e-10

See man sort for more details.

You have to make a decicsion, do you want it Reverse or not? (-r)

Hey,

thanks for the fast reply and the tips. I edited the original post and also quoted the lists as code. However, I do not only want the numbers before the e... sorted but the scientific numbers as a whole. So the order should be as in the third list of my previous post rather than the second. Under Linux, this works fine with the -g option, under mac it doesn't and -g seems equivalent to -n, where only the numbers before the e... are sorted.

Any further thoughts on this?
Thanks again.

Strange, I cannot reproduce this on OSX 10.10.3 (Yosemite). Sort works as designed and it appears to be GNU sort:

$ sort -gr file
6.38e-10
6.38e-10
1.00e-10
1.80e-11
1.48e-12

$ sort -nr file
6.38e-10
6.38e-10
1.80e-11
1.48e-12
1.00e-10

$ sort --version
sort (GNU coreutils) 5.93
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and Paul Eggert.

$ pkgutil --file-info  /usr/bin/sort
volume: /
path: /usr/bin/sort

pkgid: com.apple.pkg.BaseSystemBinaries
pkg-version: 10.10.0.1.1.1411459885
[..]

What happens when you enter:

type sort

I have the same version. "type sort" gave:

Is there a way to re-install sort?

I doubt that a gremlin has fiddled one random bit inside the executable to break general sorting. If the option isn't found that's causing it to misbehave, it'd still misbehave when reinstalled.

This is a GNU utility, meaning it tries to abide by LOCALE (when most equivalents don't) which sometimes means bugs unintended consequences related to that. What's your locale? Try LOCALE=C sort ... to prevent it from doing any unintended translation, etc.

1 Like

Bingo, that must be it:

$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=

$ sort -gr file
6.38e-10
6.38e-10
1.00e-10
1.80e-11
1.48e-12

$ LC_NUMERIC=nl_NL.UTF-8 sort -gr file
6.38e-10
6.38e-10
1.80e-11
1.48e-12
1.00e-10

With the latter locale the . is a thousands separator and the , is the decimal mark:

$ LC_NUMERIC=nl_NL.UTF-8 sort -gr <(sed 's/\./,/' file)
6,38e-10
6,38e-10
1,00e-10
1,80e-11
1,48e-12
1 Like

When it came to locale, I was out. So, I can't tell you, what my locale is. I type in locale in the terminal and got this (don't know, if that's of any help for you):

LOCALE=C sort -gr <file>

didn't change anything.

---------- Post updated at 05:01 PM ---------- Previous update was at 04:51 PM ----------

Oh bummer! That was it! It's a stupid German thing as we have to do it differently as most of the rest of the world. Is there any way that I can change the settings universily in the OS, so thousand separators are always commas and decimal points are periods?

Many thanks for the solution!

You're welcome. Try putting

export LANG=en_US.UTF-8 

in your .profile

---

Don't know about that:


Green is comma, blue is dot.
Decimal mark - Wikipedia, the free encyclopedia

Yes, that works perfectly (after I found out, I am using the bash shell and have to alter the .bash_profile).

Thanks again.