-
Notifications
You must be signed in to change notification settings - Fork 14
/
basicstats.awk
executable file
·64 lines (57 loc) · 1.24 KB
/
basicstats.awk
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
#!/usr/bin/awk -f
#
# awk script to calculate basic statistical info
#
# Pipe one numerical value per line into the script.
#
# $> ... STDIN | basicstats.awk
#
#Typical Output:
#
# Num of values: 10000
# Mean: 91306
# Median: 2431
# Min: 18
# Max: 301455050
# Range: 301455032
# Std deviation: 3023884
#
BEGIN {
sum = 0.0
sum2 = 0.0
min = 10e10
max = -min
}
(NF>0) {
sum += $1
sum2 += $1 * $1
N++;
if ($1 > max) {
max = $1
}
if ($1 < min) {
min = $1
}
arr[NR]=$1
}
END{
asort(arr)
if (NR%2==1) {
median = arr[(NR+1)/2]
}
else {
median = (arr[NR/2]+arr[NR/2+1])/2
}
if(N>0) {
printf "%14s %'18.2f\n", "Num of values:" ,N
printf "%14s %'18.2f\n", "Mean:", sum/N
printf "%14s %'18.2f\n", "Median:", median
printf "%14s %'18.2f\n", "Min:", min
printf "%14s %'18.2f\n", "Max:", max
printf "%14s %'18.2f\n", "Range:", max-min
printf "%14s %'18.2f\n", "Std deviation:", sqrt((sum2 - sum*sum/N)/N)
}
else {
print "ERROR: No non-null values found"
}
}