Results 1 to 3 of 3

Thread: sort by multiple columns not working

  1. #1
    Join Date
    Aug 2012
    Beans
    2

    sort by multiple columns not working

    I know you don't believe me, that sort is not working, but here's the situation: I have the attached file tmp1.txt. I run this command:

    $ sort -t',' -k1 -k2 -k9g -k7g -k8g -k3g -k4g -k12g -k10g -k11g -k5g -k6g -otmp2.txt tmp1.txt

    the output tmp2.txt is attached. It seems to have gotten the -k1 -k2 correct. Columns 9 and 7 are not different, but then it should sort by column 8. It does not.

    All help appreciated.
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2008
    Location
    Texas City, Texas
    Beans
    830
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: sort by multiple columns not working

    I'm not sure just what you are expecting, but it appears that sort is doing exactly what it is supposed to do.

    Sort normally begins at the beginning (left) of the line and continues to the right until it encounters a difference. Once a difference is encountered, it sorts the difference and ignores the rest of the line.

    The (key) argument causes sort to begin somewhere other than the beginning of the line. And optionally, a second (key) argument stops the sort before the end of the line.

    Depending on what you are trying to accomplish, you might possibly achieve your goal by piping the output of sort into a second sort operation.
    Break it, fix it, learn something.
    People who never make mistakes seldom make anything!

  3. #3
    Join Date
    Aug 2012
    Beans
    2

    Re: sort by multiple columns not working

    Thanks so much for the quick response, Miljet. As usual, I expected it to do what I meant, not what I said

    It's been a while and i had forgotten about the key ending. I changed my sort to:

    $ sort -t, -k1,2 -k9,9g -k7,7g -k8,8g -k3,4g -k12,12g -k10,10g -k11,11g -k5,6g -otmp2.txt tmp1.txt

    and now I get what I wanted.

    Thanks for the reminder !

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •