Sorting scientific numbers with sort


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Sorting scientific numbers with sort

Hey everybody,

I'm trying to sort scientific numbers in a descending order using the command
Code:
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:
Code:
6.38e-10
6.38e-10
1.80e-11
1.00e-10
1.48e-12

And got this:
Code:
6.38e-10
6.38e-10
1.80e-11
1.48e-12
1.00e-10

Instead of this:
Code:
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!

Last edited by plebs; 05-21-2015 at 12:17 PM..
# 2  
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:
Code:
$ 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)

Last edited by sea; 05-21-2015 at 12:23 PM..
# 3  
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.

Last edited by plebs; 05-21-2015 at 12:37 PM..
# 4  
Quote:
Originally Posted by plebs
Hey everybody,

I'm trying to sort scientific numbers in a descending order using the command
Code:
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:
Code:
6.38e-10
6.38e-10
1.80e-11
1.00e-10
1.48e-12

And got this:
Code:
6.38e-10
6.38e-10
1.80e-11
1.48e-12
1.00e-10

Instead of this:
Code:
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!
Strange, I cannot reproduce this on OSX 10.10.3 (Yosemite). Sort works as designed and it appears to be GNU sort:

Code:
$ 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:
Code:
type sort


Last edited by Scrutinizer; 05-21-2015 at 12:54 PM..
# 5  
I have the same version. "type sort" gave:

Quote:
sort is hashed (/usr/bin/sort)
Is there a way to re-install sort?
# 6  
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.

Last edited by Corona688; 05-21-2015 at 02:17 PM..
This User Gave Thanks to Corona688 For This Post:
# 7  
Bingo, that must be it:
Code:
$ 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:

Code:
$ 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


Last edited by Scrutinizer; 05-21-2015 at 01:49 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #56
Difficulty: Easy
A flip-flop circuit stores 1 bit of data.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Sorting numbers in a file

Hi Guys, Would appreciate some help on sorting numbers on a file using the sort command. I have tried this and it's not sorting properly. what am i missing? cat testing_sort 1:21 4:18 2:17 7:14 9:19 3:12 0:16 8:13 5:20 6:15 10:11 sort -t: -nk1,1 -nk2,2 testing_sort (4 Replies)
Discussion started by: Apollo
4 Replies

2. Shell Programming and Scripting

Help: Sorting the numbers without using sort command

Hi, I've got two arrays 1 3 5 7 2 4 6 8 and i need to write a shell script to get the output 1 2 3 4 5 6 7 8 without using sort or bubble sort. (1 Reply)
Discussion started by: web2moha
1 Replies

3. UNIX for Dummies Questions & Answers

Sorting files ending in numbers with "sort"

I have a group of files that I need to be sorted by number. I have tried to use the sort command without any luck. ls includes* includes1 includes10 includes11 includes12 includes2 includes3 includes4 includes5 includes6 includes7 includes8 includes9 I have tried ls includes*... (6 Replies)
Discussion started by: newbie2010
6 Replies

4. Shell Programming and Scripting

Sorting numbers containing symbols

hi all, i need help on sorting data. i have a file as below /home/oracle $ cat 234.txt +1234 -2356 -1001 +231 0023 -0987 +19000 65487 6 after sorting i want the output as below -2356 -1001 (2 Replies)
Discussion started by: mahesh1987
2 Replies

5. Shell Programming and Scripting

Is it Possible to sort a list of hexadecimal numbers using "sort" command?

Hello Everybody :) !!!. i have question in mind, is it possible to sort a list of hexadecimal numbers using "sort" command? (9 Replies)
Discussion started by: Kesavan
9 Replies

6. Shell Programming and Scripting

Sorting with header and mixed numerals (scientific and decimal) | awk

Assoc.txt CHR SNP BP A1 TEST NMISS OR STAT P 1 rs2980319 766985 A ADD 4154 1.024 0.1623 0.8711 1 rs2980319 766985 A AGECAT 4154 1.371 6.806 1.003e-11 1 ... (6 Replies)
Discussion started by: genehunter
6 Replies

7. UNIX for Dummies Questions & Answers

sorting numbers with sort -n

Looking for help for sort, I learned that for sorting numbers I use: sort -n but it seems that that is not enough when you have numbers like 0.2000E+7 for example, sort -n will not worry about the E+7 part, and will just sort the numbers like 0.2000. Exapmle: cat example.txt .91000E+07... (9 Replies)
Discussion started by: cosmologist
9 Replies

8. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

9. UNIX for Dummies Questions & Answers

sorting ASCII string containing numbers

I have the following output where I need to sort the second column numerically (starting with IBMULT3580-TD10 and ending in IBMULT3580-TD123) Drv DriveName 0 IBMULT3580-TD13 1 IBMULT3580-TD18 2 IBMULT3580-TD14 3 IBMULT3580-TD10 4 IBMULT3580-TD11 5 IBMULT3580-TD17 ... (8 Replies)
Discussion started by: GKnight
8 Replies

10. UNIX for Dummies Questions & Answers

How to add/multiply numbers with scientific notation (2.343e-5)

Hi, I'm need to do some addition and multiplication of scientific nottaion numbers, in the form 34.23423e-10 for example. I was echoing the list of numbers to stdout, then using bc -l, then I find that this does not seem to work for numbers with exponential notation. Could someone help me out... (1 Reply)
Discussion started by: chugger06
1 Replies

Featured Tech Videos