Skip to content

Instantly share code, notes, and snippets.

@JayCuthrell
Created December 10, 2014 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayCuthrell/4d20547427b4f6efb358 to your computer and use it in GitHub Desktop.
Save JayCuthrell/4d20547427b4f6efb358 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# via http://perlmaven.com/count-words-in-text-using-perl
# $ cat fruit.txt
# apple
# pear
# banana
# pear
# pear
# banana
# grape
# $ ./repeats.pl fruit.txt
# .......
# pear 3
# banana 2
# grape 1
# apple 1
use strict;
use warnings;
my %repeat;
my $export = shift or die "\n \n Nope: $0 exportfilenamehere.txt \n \n";
open my $ef, '<', $export or die "\n \n I can't find a file named '$export'\n \n $!";
while (my $items = <$ef>) {
chomp $items;
foreach my $element ( split /\s+/, $items) {
$repeat{$element}++;
printf ".";
}
}
printf "\n";
foreach my $element (reverse sort { $repeat{$a} <=> $repeat{$b} } keys %repeat) {
printf "%-10s %s\n", $element, $repeat{$element};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment