Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] sort -n, huge number
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 01-22-2011, 06:42 AM #1 kaz2100 Senior Member   Registered: Apr 2005 Location: Penguin land, with apple, no gates Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie Posts: 1,455 Rep: sort -n, huge number Hya, Question With 'sort -n': How huge can numbers be? So far, I did 1. Check man page (info page as well) -> Not quite informative. (locale may be important.) 2. Experiment Code: ```>cat testsort 1 11111 11111111111111 111111111111111111111 1111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111110 0.1 0.000000000000000000001 000.1 100.001 100.1 1.0 1.0001 1 1.00000000000000000000000000000000000000000000000000000000000000000000000000 1.00 > cat testsort | sort -n 0.000000000000000000001 000.1 0.1 1 1 1.0 1.00 1.00000000000000000000000000000000000000000000000000000000000000000000000000 1.0001 100.001 100.1 11111 11111111111111 111111111111111111111 1111111111111111111111111111111111111111111111 11111111111111111111111111111111111111111111111110 11111111111111111111111111111111111111111111111111 >``` It looks good, except for (unnecessary) leading and trailing zero. Does anybody know the answer? Happy Penguins! Last edited by kaz2100; 01-22-2011 at 07:29 AM. Reason: typo
 01-22-2011, 10:53 AM #2 Nominal Animal Senior Member   Registered: Dec 2010 Location: Finland Distribution: Xubuntu, CentOS, LFS Posts: 1,723 Blog Entries: 3 Rep: The general_numcompare() function in sort from coreutils-8.9 converts the numbers to the native long double type using the standard strtold() library function. This is used when the -g option is used. Option -n uses strnumcmp() defined in coreutils-8.9/lib/strnumcmp-in.h, which does not do any numerical conversions. It is purely string-based, and should be able to compare any decimal number strings that fit into memory. I sincirely hope this was not homework,Nominal Animal Last edited by Nominal Animal; 03-21-2011 at 07:03 AM.
 01-22-2011, 11:17 AM #3 gnashley Amigo developer   Registered: Dec 2003 Location: Germany Distribution: Slackware Posts: 4,775 Rep: I'd guess they would be limited by something like MAX_LINE -probably at least 1024 chars long -or by how much RAM you have for storing it all. You could do without the 'cat' command for faster results: Code: `sort -n testsort`
01-22-2011, 12:28 PM   #4
Nominal Animal
Senior Member

Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep:
@gnashley: Some sort implementations may have line length limits, but not GNU sort from the coreutils package. From info sort:
Quote:
 GNU `sort' (as specified for all GNU utilities) has no limit on input line length or restrictions on bytes allowed within lines. In addition, if the final byte of an input file is not a newline, GNU `sort' silently supplies one. A line's trailing newline is not part of the line for comparison purposes.
So yes, the limit seems to be the available memory (RAM + swap, or resource limits, whichever are smaller).
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 07:03 AM.

 01-23-2011, 07:12 AM #5 kaz2100 Senior Member   Registered: Apr 2005 Location: Penguin land, with apple, no gates Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie Posts: 1,455 Original Poster Rep: Hya thanks! My bad! I did not think of checking the source code. This is not my homework, it is rather my job (second job). Now I feel good. The other day, I hit the size limit of long long int in C. Of course, it did not happen in test run, and it took several days to debug. I won't run into similar problem. Happy Penguins!

 Tags coreutils, digits, precision, sort