Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

History Meme, in Perl!

by defyance (Curate)
on Apr 11, 2008 at 18:07 UTC ( [id://679801]=CUFP: 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

Replies are listed 'Best First'.
Re: History Meme, in Perl!
by kyle (Abbot) on Apr 11, 2008 at 18:54 UTC

    Wow, the number one item in my history is perl! I guess that comes from running everything as "perl blahblah.pl". Number two is cd. And it appears I use perldoc more often than man.

    Using the suggestion from rhesa,

    history|perl -lane '$h{$F[1]}++;END{print"$h{$_} $_"for(sort{$h{$b}<=> +$h{$a}}keys%h)[0..9]}'

      Shorter:

      system q(history|awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}'|s +ort -rn|head)
        bash: system: command not found

        Not only that, "perl -e 'system q(history)'" produces no output, presumably because the subshell that perl spanws for system doesn't have a history.

Re: History Meme, in Perl!
by rhesa (Vicar) on Apr 11, 2008 at 18:14 UTC
    I think you may enjoy the -a and -n command line switches.
Re: History Meme, in Perl!
by smokemachine (Hermit) on Jun 05, 2008 at 15:55 UTC
    history|perl -anE '$h{$F[1]}++;}{say"$h{$_} $_"for(sort{$h{$b}<=>$h{$a}}keys%h)[0..9]'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://679801]
Approved by rhesa
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found