#!/usr/bin/perl -w use Getopt::Std; $Getopt::Std::STANDARD_HELP_VERSION++; ($SELF = $0) =~ s,.*/,,; $VERSION="0.0.0"; sub HELP_MESSAGE { print < ... Options are: --help -h display this help message -c show counts only (suppress percentages) -p show percentages only (suppress counts) -w remove leading and trailing whitespace from input lines -W remove ALL whitespace from input lines -t show total count -e code execute perl code for each line of input (should modify \$_, make sure you quote the argument as needed by your shell) Input will be read from filenames given on the command line, or from standard input if none given. The input need not be sorted. EOF } sub VERSION_MESSAGE { print "$SELF $VERSION\n"; } getopts('hcpwWte:', \%opt); HELP_MESSAGE() if $opt{h}; die "$SELF: can't give -c and -p options together\n" if $opt{c} && $opt{p}; %counts = (); $total = 0; while(<>) { chomp; if($opt{e}) { eval $opt{e}; die "$SELF: $@" if $@; } s/^\s+|\s+$//g if $opt{w}; s/\s//g if $opt{W}; $counts{$_}++; $total++; } for(sort keys %counts) { print $_ . "\t"; print $counts{$_} . "\t" unless $opt{p}; printf "%.1f%%", ($counts{$_} * 100 / $total) unless $opt{c}; print "\n"; } if($opt{t}) { print "\n-- Total count: $total\n"; } exit(0);