http://qs321.pair.com?node_id=679801

Wow, it's been a long time since I posted!

So, there's a bit of a buzz in the tech blogging world right now regarding a line of shell code that gives you you're most 10 frequently used shell commands. It goes like this:

history|awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|h +ead

Neat trick, nothing too fancy. Soon after I read about this planet python went nuts and it's all I've seen in my Feed Reader for the last two days.

One of these posts in particular caught my eye as it's not something often seen in the python world.. A python 'one liner' to emulate the above shell script.

Huzzah! I thought; "I've already done in it in my head with Perl!" I also thought it was pretty neat.

Now, I'm not an awesome one-liner kinda guy, but I know for a fact that quite a number of you here are. So I'm going to start with mine, and see how many different/better/fun ways this can be done in Perl. Golf it up!

history | perl -le 'while($l = <>) {$n=${[split(/\s+/,$l)]}[2];$h{$n}++;} $i=0;foreach $k (reverse sort {$h{$a}<=>$h{$b}} keys %h) {last if $i > 9; print "$h{$k} $k"; $i++;}'

Slightly shorter (but probably not as efficient):
history | perl -le 'while($l = <>) {$n=${[split(/\s+/,$l)]}[2];$h{$n}+ ++;} $i=0;@a=reverse sort {$h{$a}<=>$h{$b}} keys %h; print "$h{$a[$_]} + $a[$_]" for (0..9)'

--
P.S. This is what the alphabet would look like if Q and R were eliminated.. -- Mitch Hedberg