# # 2005/05/26 sort -g vs. sort -n
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

2005/05/26 sort -g vs. sort -n

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© May 2005 Tony Lawrence

When sorting numbers, you can use either -g or -n. I've noticed that Linux users without Unix experience tend to use -g, while old Unix folk are often unaware of that flag at all and continue to use -n. There is a difference between the two flags, although "man sort" doesn't explain it - "info sort" does a better job.

For ordinary numbers, -g and -n are identical, and although -g is slower, for small input sets it really doesn't matter. But when you mix in scientific notation, -n does not work:


$ cat t
 123
12
11
9
453
99
10e1
10e0
101
1
8.95
$ sort -n t
1
8.95
9
10e0
10e1
11
12
99
101
 123
453
$ sort -g t
1
8.95
9
10e0
11
12
99
10e1
101
 123
453
 

I mentioned that "sort -g" is slower. That's because it calls "strtod" to convert to double-precision floating point. When -n is used, sort simply aligns decimal points (real or assumed) and then does an ordinary string comparison. That's much faster, but of course it fails to handle scientific notation. The "faster" is, as noted, usually unimportant:

$ wc -l t
1321 t
$ time sort -g t > /dev/null

real    0m0.013s
user    0m0.012s
sys     0m0.002s
$ time sort -n t > /dev/null

real    0m0.009s
user    0m0.008s
sys     0m0.001s
 

You may have "-g" even if your man page doesn't mention it: my Mac OS X Tiger has -g but the man page doesn't mention it.

`

If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> 2005/05/26 sort -g vs. sort -n

2 comments


Inexpensive and informative Apple related e-books:

Take Control of Apple Mail, Third Edition

Take Control of Parallels Desktop 12

Are Your Bits Flipped?

Take Control of Numbers

Take Control of Preview





More Articles by © Tony Lawrence







Tue Apr 21 12:23:30 2009: 6247   TonyLawrence

gravatar
I noticed this morning that I can no longer use "sort +2" (well, you CAN, but you have to work at it) on OS X

The "man" page doesn't mention the old "+" syntax but "info sort" still does.

(link) explains:

On older systems, sort supports an obsolete origin-zero syntax +pos1 [-pos2] for specifying sort keys. This obsolete behavior can be enabled or disabled with the _POSIX2_VERSION environment variable (see Standards conformance); it can also be enabled when POSIXLY_CORRECT is not set by using the obsolete syntax with -pos2 present.



Fri Jun 14 02:16:16 2013: 12121   KanthaRao

gravatar


Hi this is a very old post, nevertheless a good mention for the differences between -g and -n sort. I am using another command which is something like "sort +4g". It will sort out based column 4 smallest to largest.

------------------------


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





Java is the most distressing thing to hit computing since MS-DOS. (Alan Kay)




Linux posts

Troubleshooting posts


This post tagged:

Shell



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode