Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found