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
Awesome... I struggled with the same problem.
ReplyDeleteThis could so easily have been pointed out in the sort man page.
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.
ReplyDeletesort -k5gr -k2g file
Thanks once again!!
In my version of sort,
ReplyDeletesort -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.
Based on MistarOblivion's note, I used:
ReplyDeletels | 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.
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