Wednesday, March 3, 2010

UNIX sort by multiple columns

Sometimes the "obvious" stuff isn't so obvious (at least to me...)

I had a text file with multiple columns of numbers. I wanted to sort it by the first column, then the second, then the third. It wasn't immediately obvious to me how to do it. The man page for 'sort' of course does not mention that you can specify the '-k' option multiple times, and I didn't have the GNU 'info' utility installed:
sort -k1n -k2n -k3n file

5 comments:

  1. Awesome... I struggled with the same problem.
    This could so easily have been pointed out in the sort man page.

    ReplyDelete
  2. Very useful, thanks a lot. I figured out from your post how to sort on two columns, one in ascending order and the other descending.

    sort -k5gr -k2g file

    Thanks once again!!

    ReplyDelete
  3. In my version of sort,

    sort -k3n -k5n filename

    did not work! I banged my head and frothed at the mouth, but eventually I figured out that in my version of sort, you have to specify the START and END columns for each -k.

    What ended up working for me was:

    sort -k3,3n -k5,5n filename


    sort --version
    sort (GNU coreutils) 6.10
    Copyright (C) 2008 Free Software Foundation, Inc.

    FYI, hope this helps someone else save an hour or two of headbanging.

    ReplyDelete
  4. Based on MistarOblivion's note, I used:

    ls | sort -k1,1 -k3,3n

    to sort the following file names (example)
    M - 9(fld 1 wv TL-Brightfield - DAPI).tif
    M - 9(fld 1 wv TexasRed - TexasRed).tif
    N - 1(fld 1 wv DAPI - DAPI).tif
    N - 1(fld 1 wv TL-Brightfield - DAPI).tif
    N - 1(fld 1 wv TexasRed - TexasRed).tif
    N - 10(fld 1 wv DAPI - DAPI).tif
    N - 10(fld 1 wv TL-Brightfield - DAPI).tif
    N - 10(fld 1 wv TexasRed - TexasRed).tif

    first based on the column 1 (capital letter), then on column 3 using a numeric sort. Life saver!
    Thanks.

    ReplyDelete
  5. The problems with params is having the "n" at the end. I'd rather use -nk 3 -nk 5. That should be backwards compatible. My $0.02.

    ReplyDelete