aboutsummaryrefslogtreecommitdiff
path: root/bkt
blob: 42ea6eafb789238a630d35490d05ee41f2970db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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);