File: sawk.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 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 [options...] [awk expression...] [files...]
  27 #
  28 #
  29 # Summarize via AWK calculates some numeric statistics from an AWK expression.
  30 #
  31 # The handy case-insensitive shortcut options may cause this tool to fail,
  32 # if the main AWK tool installed doesn't support the special IGNORECASE
  33 # variable.
  34 #
  35 # The AWK options available only in single-dash versions are
  36 #
  37 #   -F fs, -Ffs, -F=fs    make `fs` the field separator
  38 #
  39 # The other options are, available both in single and double-dash versions
  40 #
  41 #   -h, -help    show this help message
  42 #   -i, -ins     match regexes case-insensitively; may fail the default `awk`
  43 #   -tsv         split fields using tabs, same as using -F "\t"
  44 
  45 
  46 case "$1" in
  47     -h|--h|-help|--help)
  48         awk '/^# +sawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  49         exit 0
  50     ;;
  51 esac
  52 
  53 tsv=0
  54 case_insensitive=0
  55 command='awk'
  56 
  57 while [ $# -gt 0 ]; do
  58     case "$1" in
  59         -F)
  60             if [ $# -lt 2 ]; then
  61                 printf "expected value after -F option\n" >&2
  62                 exit 1
  63             fi
  64             command="${command} -F $2"; shift 2; continue
  65         ;;
  66 
  67         -F*) command="${command} $1"; shift; continue ;;
  68 
  69         -i|--i|-ins|--ins|-insensitive|--insensitive)
  70             case_insensitive=1; shift; continue
  71         ;;
  72 
  73         -tsv|--tsv) tsv=1; shift; continue ;;
  74 
  75         -v)
  76             if [ $# -lt 2 ]; then
  77                 printf "expected variable assignment after -v option\n" >&2
  78                 exit 1
  79             fi
  80             command="${command} -v $1"; shift 2; continue
  81         ;;
  82 
  83         -) break ;;
  84 
  85         --) shift; break ;;
  86 
  87         -*)
  88             printf "unsupported option '%s'\n" "$1" >&2
  89             exit 1
  90         ;;
  91     esac
  92 
  93     break
  94 done
  95 
  96 code="${1:-\$0}"
  97 [ $# -gt 0 ] && shift
  98 
  99 # show all non-existing files given
 100 failed=0
 101 for arg in "$@"; do
 102     [ "${arg}" = "-" ] && continue
 103     [ -e "${arg}" ] && continue
 104     printf "no file named \"%s\"\n" "${arg}" >&2
 105     failed=1
 106 done
 107 
 108 [ "${failed}" -gt 0 ] && exit 2
 109 
 110 ci='
 111     BEGIN {
 112         if (IGNORECASE == "") {
 113             m = "your `awk` command lacks case-insensitive regex-matching"
 114             print(m) > "/dev/stderr"
 115             exit 125
 116         }
 117         IGNORECASE = 1
 118     }
 119 '
 120 if [ "${case_insensitive}" -eq 0 ]; then
 121     ci=''
 122 fi
 123 
 124 src="${ci}"'
 125     BEGIN {
 126         numeric = ints = pos = zero = neg = 0
 127 
 128         inf = "+inf" + 0
 129 
 130         min = inf
 131         max = -inf
 132         sum = 0
 133         mean = 0
 134         prod = 1
 135     }
 136 
 137     {
 138         v = ('"${code}"')
 139         if (v !~ /^ *(0|[0-9]+|[0-9]*\.[0-9]+) *$/) next
 140         v = v + 0
 141 
 142         numeric++
 143         ints += v % 1 == 0
 144         if (v > 0) pos++
 145         else if (v < 0) neg++
 146         else if (v == 0) zero++
 147 
 148         min = min < v ? min : v
 149         max = max > v ? max : v
 150         sum += v
 151         prod *= v
 152         lnSum += v <= 0 ? -inf : log(v)
 153 
 154         # advance welford`s algorithm
 155         d1 = v - mean
 156         mean += d1 / numeric
 157         d2 = v - mean
 158         meanSq += d1 * d2
 159     }
 160 
 161     END {
 162         sum = mean * numeric
 163         if (numeric == 0) lnSum = -inf
 164 
 165         # separate name-value pairs using tabs, and prepare a
 166         # pipeable command which ignores all-zero decimals
 167         OFS = "\t"
 168 
 169         print "numeric", numeric
 170         if (numeric > 0) {
 171             print "min", sprintf("%f", min)
 172             print "max", sprintf("%f", max)
 173             print "sum", sprintf("%f", sum)
 174             print "mean", sprintf("%f", mean)
 175             print "geomean", (zero == 0 && neg == 0) ?
 176                 sprintf("%f", exp(lnSum / numeric)) :
 177                 ""
 178             print "sd", sprintf("%f", sqrt(meanSq / numeric))
 179             print "product", sprintf("%g", prod)
 180         } else {
 181             print "min", ""
 182             print "max", ""
 183             print "sum", ""
 184             print "mean", ""
 185             print "geomean", ""
 186             print "sd", ""
 187             print "product", ""
 188         }
 189         print "integer", ints
 190         print "positive", pos
 191         print "zero", zero
 192         print "negative", neg
 193     }
 194 '
 195 
 196 if [ "${tsv}" -eq 1 ]; then
 197     ${command} -F "\t" "${src}" "$@"
 198 else
 199     ${command} "${src}" "$@"
 200 fi