File: sawk.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 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 # show all non-existing files given 46 failed=0 47 for arg in "$@"; do 48 if [ "${arg}" = "-" ]; then 49 continue 50 fi 51 if [ ! -e "${arg}" ]; then 52 printf "no file named \"%s\"\n" "${arg}" > /dev/stderr 53 failed=1 54 fi 55 done 56 57 if [ "${failed}" -gt 0 ]; then 58 exit 2 59 fi 60 61 awk ' 62 BEGIN { 63 numeric = ints = pos = zero = neg = 0 64 65 inf = "+inf" + 0 66 67 min = inf 68 max = -inf 69 sum = 0 70 mean = 0 71 prod = 1 72 } 73 74 FNR == 1 { FS = /\t/ ? "\t" : " "; $0 = $0 } 75 { low = lower = tolower($0) } 76 77 { 78 v = '"${code}"' 79 if (v !~ /^ *(0|[0-9]+|[0-9]*\.[0-9]+) *$/) next 80 v = v + 0 81 82 numeric++ 83 ints += v % 1 == 0 84 if (v > 0) pos++ 85 else if (v < 0) neg++ 86 else if (v == 0) zero++ 87 88 min = min < v ? min : v 89 max = max > v ? max : v 90 sum += v 91 prod *= v 92 lnSum += v <= 0 ? -inf : log(v) 93 94 # advance welford`s algorithm 95 d1 = v - mean 96 mean += d1 / numeric 97 d2 = v - mean 98 meanSq += d1 * d2 99 } 100 101 END { 102 sum = mean * numeric 103 if (numeric == 0) lnSum = -inf 104 105 # separate name-value pairs using tabs, and prepare a 106 # pipeable command which ignores all-zero decimals 107 OFS = "\t" 108 109 print "numeric", numeric 110 if (numeric > 0) { 111 print "min", sprintf("%f", min) 112 print "max", sprintf("%f", max) 113 print "sum", sprintf("%f", sum) 114 print "mean", sprintf("%f", mean) 115 print "geomean", (zero == 0 && neg == 0) ? 116 sprintf("%f", exp(lnSum / numeric)) : 117 "" 118 print "sd", sprintf("%f", sqrt(meanSq / numeric)) 119 print "product", sprintf("%g", prod) 120 } else { 121 print "min", "" 122 print "max", "" 123 print "sum", "" 124 print "mean", "" 125 print "geomean", "" 126 print "sd", "" 127 print "product", "" 128 } 129 print "integer", ints 130 print "positive", pos 131 print "zero", zero 132 print "negative", neg 133 } 134 ' "$@" | sed -E 's-([0-9]+)\.0+$-\1-g; s-([0-9]+\.[0-9]*[1-9])0+$-\1-g'