forked from mjpost/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram
executable file
·110 lines (103 loc) · 2.98 KB
/
histogram
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/sh
# histogram --- create an ASCII histogram of a column of the input
# Copyright (C) 2000-2001 Riku Saikkonen
#
# Author: Riku Saikkonen <[email protected]>
# Version: 1.0 (October 28th, 2001)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Requires: a sh-compatible shell and GNU Awk
#
# Syntax: histogram [-c] [COLUMN [MIN [MAX [STEP [PREC]]]]]
# If MIN, MAX, STEP and PREC are specified, count from MIN to MAX in
# steps of STEP, showing PREC digits after the decimal point.
# -c = also print cumulative sums
#
# Defaults: COLUMN = 1 (first column)
# only show the values that are listed (no MIN, MAX, STEP, PREC)
# MAX = 6, STEP = 0.5, PREC = 1
#
# Examples: histogram <datafile
# histogram 2 <datafile
# histogram 5 0 <datafile
# histogram 4 0 5 0.1 1 <datafile
# Change log:
# $Id: histogram,v 1.2 2001/10/28 21:39:46 rjs Exp $
#
# Version 1.0 (October 28th, 2001)
# * Initial public release.
column=1
min=0
max=6
step=0.5
prec=1
hasstep=0
cumulative=0
if [ "$1" = "-c" ]; then
cumulative=1
shift
fi
[ "$1" != "" ] && column="$1"
[ "$2" != "" ] && min="$2"
[ "$3" != "" ] && max="$3"
[ "$4" != "" ] && step="$4"
[ "$5" != "" ] && prec="$5"
[ "$2" != "" ] && hasstep=1
if [ "$cumulative" = 1 -a "$hasstep" = 0 ]; then
echo "Error: Using -c without MIN,MAX,STEP not supported!" 1>&2
exit 1
fi
gawk '
function printline(key, value, numerickey, j) {
csum += value;
if (numerickey)
printf("%s\t", key);
else
printf("%.*f\t", ('"$prec"'), key);
if ('"$cumulative"')
printf("%d\t%5.1f%%\t", csum, 100.0*csum/sumh);
printf("%d\t%5.1f%%\t", value, 100.0*value/sumh);
for (j = 0; j < (columns-1-(8*(3+2*'"$cumulative"')))*value/maxh; j++)
printf("*");
print "";
}
BEGIN {
columns = ENVIRON["COLUMNS"]+0;
if (columns < 1)
columns = 80;
}
{
data = $'"$column"';
if ('"$hasstep"') {
data += 0;
if (data < ('"$min"') || data > ('"$max"') ||
(data-('"$min"'))%('"$step"') != 0)
print "Error: invalid data: " data >"/dev/stderr";
}
h[data]++;
sumh++;
if (h[data] > maxh)
maxh = h[data];
}
END {
csum = 0;
if ('"$hasstep"') {
for (i = ('"$min"'); i <= ('"$max"'); i += ('"$step"'))
printline(i, h[i], 1);
} else {
for (i in h)
printline(i, h[i], 0);
}
}' | sort -n