diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-09-02 14:13:13 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-09-02 14:21:21 +1200 |
commit | 3909483124a95893429c437336d9812596591c79 (patch) | |
tree | 85e18c0eb60e2ade16f59a50f4b059f2d26cadcc /bin | |
parent | Add an issue with the Mutt configuration (diff) | |
download | dotfiles-3909483124a95893429c437336d9812596591c79.tar.gz dotfiles-3909483124a95893429c437336d9812596591c79.zip |
Add mean(1df), med(1df), and mode(1df)
Diffstat (limited to 'bin')
-rw-r--r-- | bin/mean.awk | 8 | ||||
-rw-r--r-- | bin/med.awk | 19 | ||||
-rw-r--r-- | bin/mode.awk | 13 |
3 files changed, 40 insertions, 0 deletions
diff --git a/bin/mean.awk b/bin/mean.awk new file mode 100644 index 00000000..4506b3b0 --- /dev/null +++ b/bin/mean.awk @@ -0,0 +1,8 @@ +# Get the mean of a list of integers +{ tot += $1 } +END { + # Error out if we read no values at all + if (!NR) + exit(1) + printf "%u\n", tot / NR +} diff --git a/bin/med.awk b/bin/med.awk new file mode 100644 index 00000000..b4a899a1 --- /dev/null +++ b/bin/med.awk @@ -0,0 +1,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 + } + printf "%u\n", med + if (warn) + exit(1) +} diff --git a/bin/mode.awk b/bin/mode.awk new file mode 100644 index 00000000..beced1f4 --- /dev/null +++ b/bin/mode.awk @@ -0,0 +1,13 @@ +# Get mode of a list of integers +# If the distribution is multimodal, the first mode is used +{ vals[$1]++ } +END { + # Error out if we read no values at all + if (!NR) + exit(1) + mode = vals[0] + for (val in vals) + if (vals[val] > vals[mode]) + mode = val + printf "%u\n", mode +} |