4

I'm trying to use GNU sort on a directory/file listing, eg (subset of much longer listing):

exams2008/Zoology/Summer/ZY4019Sum08.pdf
exams2009/Zoology/Summer/ZY4019.pdf
exams2010/Zoology/Summer/ZY4019Sum2010.pdf
exams2011/Zoology/Summer/ZY4019Sum2011.pdf
exams2012/Zoology/Autumn/ZY4019Aut2012.pdf
exams2012/Zoology/Summer/ZY4019Sum2012.pdf
exams2013/Zoology/Autumn/ZY4019Aut2013.pdf
exams2013/Zoology/Summer/ZY4019Sum2013.pdf
exams2014/Zoology/Summer/ZY4019Sum2014.pdf

I am already sorting this successfully on the filename (4th field) followed by the season (3rd field), eg

$ sort -t/ -k4 -k3,3 
exams2012/Zoology/Autumn/ZY4019Aut2012.pdf
exams2013/Zoology/Autumn/ZY4019Aut2013.pdf
exams2009/Zoology/Summer/ZY4019.pdf
exams2008/Zoology/Summer/ZY4019Sum08.pdf
exams2010/Zoology/Summer/ZY4019Sum2010.pdf
exams2011/Zoology/Summer/ZY4019Sum2011.pdf
exams2012/Zoology/Summer/ZY4019Sum2012.pdf
exams2013/Zoology/Summer/ZY4019Sum2013.pdf
exams2014/Zoology/Summer/ZY4019Sum2014.pdf

But for each block like this, I need them in descending year order as the third sort criterion, using the four digits from position 6 thru 9 of the first field. If I use:

sort -t/ -k4 -k3,3 -k1.6nr,1.9 

which looks as if it ought to work, but it does not sort that third criterion in reverse numeric order:

exams2012/Zoology/Autumn/ZY4019Aut2012.pdf
exams2013/Zoology/Autumn/ZY4019Aut2013.pdf
exams2009/Zoology/Summer/ZY4019.pdf
exams2008/Zoology/Summer/ZY4019Sum08.pdf
exams2010/Zoology/Summer/ZY4019Sum2010.pdf
exams2011/Zoology/Summer/ZY4019Sum2011.pdf
exams2012/Zoology/Summer/ZY4019Sum2012.pdf
exams2013/Zoology/Summer/ZY4019Sum2013.pdf
exams2014/Zoology/Summer/ZY4019Sum2014.pdf

I'm clearly doing something wrong here, but related questions don't address this specific condition (third sort criterion, subfield, numeric, reverse) and I can't identify a reason why it fails.

2
  • That's all equivalent to sort -t/ -k4. That sort key is different for all lines, so no other key are considered. Commented Jun 10, 2015 at 10:14
  • 1
    Sounds to me like you want: sort -t/ -k3,3 -k1,1r Commented Jun 10, 2015 at 10:16

1 Answer 1

4

To make sure I added a new line in the example data:

exams2008/Zoology/Summer/ZY4019Sum08.pdf
exams2009/Zoology/Summer/ZY4019.pdf
exams2010/Zoology/Summer/ZY4019Sum2010.pdf
exams2011/Zoology/Summer/ZY4019Sum2011.pdf
exams2012/Zoology/Autumn/ZY4019Aut2012.pdf
exams2012/Zoology/Summer/ZY4019Sum2012.pdf
exams2013/Zoology/Autumn/ZY4019Aut2013.pdf
exams2013/Zoology/Summer/ZY4019Sum2013.pdf
exams2014/Zoology/Summer/ZY4019Sum2014.pdf
exams2014/Zoology/Summer/ZY4029Sum2014.pdf <- This is added by me

I added that to illustrate that the sorting also depends on the 4th field. It has the same year and the same 3rd field, but different 4th field. If I understood correctly, that should do what you want.

sort -t/ -k3,3 -k1.6nr,1.9 -k4 file

Output:

exams2013/Zoology/Autumn/ZY4019Aut2013.pdf
exams2012/Zoology/Autumn/ZY4019Aut2012.pdf
exams2014/Zoology/Summer/ZY4019Sum2014.pdf
exams2014/Zoology/Summer/ZY4029Sum2014.pdf
exams2013/Zoology/Summer/ZY4019Sum2013.pdf
exams2012/Zoology/Summer/ZY4019Sum2012.pdf
exams2011/Zoology/Summer/ZY4019Sum2011.pdf
exams2010/Zoology/Summer/ZY4019Sum2010.pdf
exams2009/Zoology/Summer/ZY4019.pdf
exams2008/Zoology/Summer/ZY4019Sum08.pdf

First sort the 3rd field (-k3,3), then the first field chars 6-9 should be reverse and numeric (-k1.6nr,1.9), and at last the 4th field (-k4).

2
  • Very close, thank you. In fact as I said, that was a fragment of a much larger listing -- the overall dataset must primarily be in the order of the first 6 chars of the last field. But I'll work on it: it still doesn't explain why specifying the subfield-key as the last sort parameter makes it fail, though. A bug? Commented Jun 10, 2015 at 15:35
  • Hah. Bug. If I change the first sort key to make it use a subfield of the last field (4.1,4.6) then the program honors the third sort key correctly. Commented Jun 10, 2015 at 15:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .