one liner history command counter

Wondering what commands you use the most, try this one liner:

$ history | perl -ne 'END { map {print ++$i.": $_\n";} splice(@{[sort {$h{$b}<=>$h{$a}} (keys %h)]},0,5); } m/\s+\d+\s+(.*)/; $h{$1}++;'

In one of the servers I use I got:

1: ls
2: fg
3: cd ..
4: sudo tail -f /var/log/httpd/error_log
5: cd

Well, I actually added:

alias j=jobs
alias vl='sudo tail -f /var/log/httpd/error_log'

To my .bashrc after this.


Take a look at the -a option for Perl in the perlrun page. It takes care of the splitting for you. You'll get your args in @F, so you don't have to do that regex to get your fields split apart.

For giggles, I've golfed it a wee bit:

$ history | perl -anE'END{say++$i.": $_ ($h{$_})"for(sort{$h{$b}$h{$a}}%h)[0..4]}++$h{join$",@F[1..$#F]}'

Ooops... Margins are cutting part of the code.

$ history | perl -anE'END{say++$i.": $_ ($h{$_})"\

If you want to golf this, Unix > Perl:

history|awk '{print $2}'|sort|uniq -c|sort -rn|head

Leave a comment

About smash

user-pic I blog about Perl.