File: sawk.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 # sawk [awk expression...] [filenames...]
  27 #
  28 # Summarize via AWK calculates simple numeric statistics from the AWK
  29 # expression given, using each input line as a data source. Welford's
  30 # algorithm is used for improved accuracy.
  31 
  32 
  33 case "$1" in
  34     -h|--h|-help|--help)
  35         awk '/^# +sawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  36         exit 0
  37     ;;
  38 esac
  39 
  40 [ "$1" = "--" ] && shift
  41 
  42 code="${1:-\$0}"
  43 [ $# -gt 0 ] && shift
  44 
  45 awk '
  46     BEGIN {
  47         numeric = ints = pos = zero = neg = 0
  48 
  49         inf = "+inf" + 0
  50 
  51         min = inf
  52         max = -inf
  53         sum = 0
  54         mean = 0
  55         prod = 1
  56     }
  57 
  58     {
  59         v = '"${code}"'
  60         if (v !~ /^ *(0|[0-9]+|[0-9]*\.[0-9]+) *$/) next
  61         v = v + 0
  62 
  63         numeric++
  64         ints += v % 1 == 0
  65         if (v > 0) pos++
  66         else if (v < 0) neg++
  67         else if (v == 0) zero++
  68 
  69         min = min < v ? min : v
  70         max = max > v ? max : v
  71         sum += v
  72         prod *= v
  73         lnSum += v <= 0 ? -inf : log(v)
  74 
  75         # advance welford`s algorithm
  76         d1 = v - mean
  77         mean += d1 / numeric
  78         d2 = v - mean
  79         meanSq += d1 * d2
  80     }
  81 
  82     END {
  83         sum = mean * numeric
  84         if (numeric == 0) lnSum = -inf
  85 
  86         # separate name-value pairs using tabs, and prepare a
  87         # pipeable command which ignores all-zero decimals
  88         OFS = "\t"
  89 
  90         print "numeric", numeric
  91         if (numeric > 0) {
  92             print "min", sprintf("%f", min)
  93             print "max", sprintf("%f", max)
  94             print "sum", sprintf("%f", sum)
  95             print "mean", sprintf("%f", mean)
  96             print "geomean", (zero == 0 && neg == 0) ?
  97                 sprintf("%f", exp(lnSum / numeric)) :
  98                 ""
  99             print "sd", sprintf("%f", sqrt(meanSq / numeric))
 100             print "product", sprintf("%g", prod)
 101         } else {
 102             print "min", ""
 103             print "max", ""
 104             print "sum", ""
 105             print "mean", ""
 106             print "geomean", ""
 107             print "sd", ""
 108             print "product", ""
 109         }
 110         print "integer", ints
 111         print "positive", pos
 112         print "zero", zero
 113         print "negative", neg
 114     }
 115 ' "$@" | sed -E 's-([0-9]+)\.0+$-\1-g; s-([0-9]+\.[0-9]*[1-9])0+$-\1-g'