blob: 34d81c41602f12bc84c138a188bab37926c1d1f8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# Get the median of a list of integers; if it has to average it, it uses the
# integer floor of the result
{ vals[NR] = $1 }
NR > 1 && vals[NR] < vals[NR-1] && !warn++ {
printf "med: Input not sorted!\n" > "/dev/stderr"
}
END {
# Error out if we read no values at all
if (!NR)
exit(1)
if (NR % 2) {
med = vals[(NR+1)/2]
} else {
med = (vals[NR/2] + vals[NR/2+1]) / 2
}
print med
if (warn)
exit(1)
}
|