Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Another way of looking at is that the sort command treats strings of numbers like strings of letters. It sorts them not according to their value but according to their postion in the "alphabet" (0-9 instead of a-z) in the way that smallpond described it. So 1025 comes before 125 for the same reason that bacf comes before bcf in normal alphabetization.
Hope that's clear.
jdk
As you've been told three times already, it sorts it alphabetically. 1 comes before 2, 2 comes before 3, etc. It does this on individual characters, NOT on the entire number. If two lines start with the same character, it moves to the second character, then the third, same way you alphabetize any list.
Start by just looking at the first character
Code:
$ sort file5
1
1
1
2
2
4
4
Ok, there are three 1s, so move to the next character and what do you get
Code:
0
2
5
How about the 2s
Code:
2
3
And the 456s are the same, so their "order" doesn't matter.
Last edited by suicidaleggroll; 04-10-2014 at 02:45 PM.
Hi All ;
Clearly i have posted , what is the logic behind for my output . I am NOT asking abt -n option. Please provide answers for my EXACT Question
Please READ AND UNDERSTAND the answers you have been given. smallpond, suicidaleggroll, and jdkaye have ALL told you exactly why you're getting that output. You were also pointed to the man page, and as far as I know, there is NOTHING keeping you from going to Google and doing further research, or even downloading the source code for sort and looking at it.
If you don't like the answers you get here, you can always ask elsewhere...perhaps they can fit your 'exact' needs. If you don't UNDERSTAND the answers you get here, being snotty sure won't get you much further.
Here is a simplification of the excellent explanation of jdkaye.
Code:
$ sort file5
1025
125
15
225
23
456
456
You are thinking that 1025 is greater than 125 because you are
reading 1025 as one thousand twenty five, and 125 as one hundred
twenty five.
The point here is that sort function doesn't read those numbers
the way you are reading them.
sort function is comparing these numbers digit by digit.
First it compared 1 from 1025 with 1 from 125, and found both are
equal,
then it compared 0 from 1025 with 2 from 125 and found that 0 is
smaller than 2, hence it placed 1025 ahead of 125.
Last edited by TheIndependentAquarius; 04-11-2014 at 05:36 AM.
It used to be called "sort-merge". That should explain the internal logic to you. This can be another thread. Briefly the `sort` command assumes line by line left to right character based sorting unless you give options. The `man` says it simply. "SORT LINES OF TEXT FILES".
You really should READ THE MANUAL.
SO AS OTHERS HAVE SAID REPEATEDLY, UNLESS YOU USE OPTIONS, SORT ASSUMES THAT THE LINES TO BE SORTED ARE CHARACTERS. UNLIKE NUMBERS AND DIGITS, THE ASCII VALUE USUALLY DETERMINES THE SEQUENCE.
You can use options to change the column delimiter, column number and nature of data to be sorted.
Where the second line is the octal value for each byte of the input.
Sort uses the values from the second line to sort by. And doing that means that 060 (the second byte of the first line) is less than 062 (the second byte of the second line), thus the first line is placed first.
Using the -n option to sort causes sort to first convert the data into integers.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.