aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB. Watson <yalhcru@gmail.com>2015-10-07 17:33:20 -0400
committerB. Watson <yalhcru@gmail.com>2015-10-07 17:33:20 -0400
commit19332446e2edfb34b998d5d14ff734c8f0660d1d (patch)
tree768b2a5887711725fa1c4f036730dee697aa8820
parent122f3c401f23f84799802c7b9667bda222646487 (diff)
downloadmisc-scripts-19332446e2edfb34b998d5d14ff734c8f0660d1d.tar.gz
bkt (bucketizer) initial commit
-rwxr-xr-xbkt82
1 files changed, 82 insertions, 0 deletions
diff --git a/bkt b/bkt
new file mode 100755
index 0000000..42ea6ea
--- /dev/null
+++ b/bkt
@@ -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);