unexpected sort behaviour

Posted on 2011-05-12
Last Modified: 2012-05-11
Hi again

I'm having more troubles with the sort command. I tried to sort my data numerically by the first column and then numerically again by the second column

  sort -k1,1n -k2,2n <<END
> 004.1 35
> 004.100 12
> 004.10000 6
004.10000 6
004.100 12
004.1 35

As you can see the first column is not sorted. 004.1 should be before 004.100. The data seems to be in numerical order by the second column. It must be due to the leading zeros as this command works fine on 'normal' numberical data

sort -k1,1n -k2,2n <<END
1 35
100 12
10000 6
1 37


1 35
1 37
100 12
10000 6

Question by:andieje
    LVL 31

    Accepted Solution

    It is doing it correctly.

    >> 004.1 should be before 004.100

    NO, numerically they are equal so it then sorts by the second column.

    Author Comment

    it isn't working properly - here is another example of the output

    004.100   245259  
    004.100   245756  
    004.1     246576  
    004.1     246611  
    004.1     246951  
    004.1     248137
    .004.1     248372  
    .004.100   250545

    if what you are saying is correct all of the '100s' should be before the '1s'

    Author Comment

    oh sorry i misread what you said. yes you are right

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now