Predictable
Stephen Hahn's blog at Sun Microsystems
All | Pastime | Person | Peruse | Position | Process | Product

« Previous day (Jul 1, 2004) | Main | Next day (Jul 3, 2004) »
20040702 Friday July 02, 2004

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 (c) 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!

(2004-07-02 11:13:25.0) Permalink Comments [1]

sort(1) puzzle #1

(I'm waiting for a build to finish, so here's a small Solaris sort(1) trick.)

Question: I have the following data file

1401 8192
1401 3487
1401 0807
1305 3471 
1305 6565
1408 2312
1408 1233

Using only sort(1), how do I generate a file sorted by the first field, with only the highest valued second field for each first field value?

Note that the unique line behaviour of sort(1) isn't well specified, so versions from other platforms may not be able to do this trick.

(2004-07-02 01:26:00.0) Permalink Comments [2]
Stephen Hahn
Sun Microsystems
sch@sun.com
17 Network Circle
MS MPK17-301
Menlo Park CA 94025 USA