Playing Perl: Counting Occurrences

Ever have a list of phrases and wonder which individual words appear the most? Me too! Here’s a handy Perl command that will get the job done:

perl -F\t -lane "map{$w{$_}++} split (/ /,$F[0]); END { print qq|$_\t$w{$_}| foreach sort{$w{$b}<=>$w{$a}} keys(%w) } "

This assumes:

  • the input contains a TAB separated list of fields
  • the first field is the one containing our keyword phrase

Good Luck!

Leave a Reply