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