diff options
author | B. Watson <yalhcru@gmail.com> | 2015-10-07 17:33:20 -0400 |
---|---|---|
committer | B. Watson <yalhcru@gmail.com> | 2015-10-07 17:33:20 -0400 |
commit | 19332446e2edfb34b998d5d14ff734c8f0660d1d (patch) | |
tree | 768b2a5887711725fa1c4f036730dee697aa8820 | |
parent | 122f3c401f23f84799802c7b9667bda222646487 (diff) | |
download | misc-scripts-19332446e2edfb34b998d5d14ff734c8f0660d1d.tar.gz |
bkt (bucketizer) initial commit
-rwxr-xr-x | bkt | 82 |
1 files changed, 82 insertions, 0 deletions
@@ -0,0 +1,82 @@ +#!/usr/bin/perl -w + +use Getopt::Std; +$Getopt::Std::STANDARD_HELP_VERSION++; + +($SELF = $0) =~ s,.*/,,; +$VERSION="0.0.0"; + +sub HELP_MESSAGE { + print <<EOF; +$SELF - count repeats in input + +Given the following input: + +foo +foo +bar +bar +baz + +$SELF will output: + +bar 2 40.0% +baz 1 20.0% +foo 2 40.0% + +Usage: $SELF <options> <file> ... + +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); |