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


In reply to History Meme, in Perl! by defyance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.