sort command in linux
Hi ;
I have a dobut. How sort works ? i don't understand the logic for numeric sortings. PHP Code:
PHP Code:
|
First columns are in order. Where first columns are the same, second columns are in order, etc.
|
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 |
In addition to the above, if you want to sort them numerically use the "-n" switch.
|
its in lexicographical order.
check man sort. if you want it in numerical order use the -n option. |
Hi ;
Thanks jdkaye ; Could you please elaborate some better example ? I am NOT clear. Some confusions. |
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 |
Why are you getting so fussy?
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 Code:
0 Code:
2 |
Quote:
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 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. |
just try to replace 0->a, 1->b, 2->c and so on, sort those strings and replace again a->0, b->1, ...
|
Quote:
Quote:
jdk |
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. OK |
People, he may be having trouble with English.
Don't think of the file as "numbers" they aren't. Your sequence Code:
1025 is actually the following sequence of bytes: Code:
0000000 1 0 2 5 \n 1 2 5 \n 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. |
All times are GMT -5. The time now is 09:24 PM. |