Answer to sort(1) puzzle #1

/usr/bin/sort -ur -k 1,1n -k1,2nr input.d

Well, I underspecified that problem slightly; the output I was looking for is

1305 6565
1401 8192
1408 2312

which the anonymous poster’s invocation will give. Alan’s invocation gets the correct line, but has the first field backwards (if I had specified the problem fully). You could, of course, send Alan’s output through another sort(1) stage to order the first field. The key to this puzzle is knowing that (a) sort(1) does a final comparison of the entire line using strcoll(3C), (b) that fields with specific modifiers ignore global modifiers (like the -r option here), and © that the Solaris implementation of sort(1) will output only the first unique line it finds in the collated sequence. The first two of these points are in the manual page; the last requires some experimenting.

True story: This puzzle grew out of a service request where a customer was moving from a platform where the last unique line was the one displayed and needed to modify their script to produce the same output on Solaris.

Please comment if you want more puzzles, or if you think I should stop before getting started!