File: sn.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # sn [filepaths...]
  27 #
  28 # Summarize Numbers does what it says, using all numbers detected from the
  29 # input(s). When given no filepaths, it reads from standard input.
  30 
  31 
  32 case "$1" in
  33     -h|--h|-help|--help)
  34         awk '/^# +sn /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  35         exit 0
  36     ;;
  37 esac
  38 
  39 awk '
  40     BEGIN {
  41         numeric = ints = pos = zero = neg = 0
  42 
  43         inf = "+inf" + 0
  44 
  45         min = inf
  46         max = -inf
  47         sum = 0
  48         mean = 0
  49         prod = 1
  50     }
  51 
  52     # update stats using all detected numbers from every line
  53     {
  54         for (i = 1; i <= NF; i++) {
  55             s = $i
  56             if (s == "" || s ~ / +/ || s ~ /[^0-9\.-]/) {
  57                 continue
  58             }
  59             v = s + 0
  60 
  61             numeric++
  62             ints += v % 1 == 0
  63             if (v > 0) pos++
  64             else if (v < 0) neg++
  65             else if (v == 0) zero++
  66 
  67             min = min < v ? min : v
  68             max = max > v ? max : v
  69             sum += v
  70             prod *= v
  71             lnSum += v <= 0 ? -inf : log(v)
  72 
  73             # advance welford`s algorithm
  74             d1 = v - mean
  75             mean += d1 / numeric
  76             d2 = v - mean
  77             meanSq += d1 * d2
  78         }
  79     }
  80 
  81     END {
  82         sum = mean * numeric
  83         if (numeric == 0) lnSum = -inf
  84 
  85         # separate name-value pairs using tabs, and prepare a
  86         # pipeable command which ignores all-zero decimals
  87         OFS = "\t"
  88 
  89         print "numeric", numeric
  90         if (numeric > 0) {
  91             print "min", sprintf("%f", min)
  92             print "max", sprintf("%f", max)
  93             print "sum", sprintf("%f", sum)
  94             print "mean", sprintf("%f", mean)
  95             print "geomean", (zero == 0 && neg == 0) ?
  96                 sprintf("%f", exp(lnSum / numeric)) :
  97                 ""
  98             print "sd", sprintf("%f", sqrt(meanSq / numeric))
  99             print "product", sprintf("%g", prod)
 100         } else {
 101             print "min", ""
 102             print "max", ""
 103             print "sum", ""
 104             print "mean", ""
 105             print "geomean", ""
 106             print "sd", ""
 107             print "product", ""
 108         }
 109         print "integer", ints
 110         print "positive", pos
 111         print "zero", zero
 112         print "negative", neg
 113     }
 114 ' "$@" | sed -E 's-([0-9]+)\.0+$-\1-g; s-([0-9]+\.[0-9]*[1-9])0+$-\1-g'