File: sn.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2024 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 # handle help options 33 case "$1" in 34 -h|--h|-help|--help) 35 awk '/^# +sn/, /^$/ { gsub(/^# ?/, ""); print }' "$0" 36 exit 0 37 ;; 38 esac 39 40 awk ' 41 # initialize stats 42 BEGIN { 43 inf = "+inf" + 0 44 45 min = inf 46 max = -inf 47 count = 0 48 sum = 0 49 mean = 0 50 prod = 1 51 52 ints = 0 53 pos = 0 54 zero = 0 55 neg = 0 56 } 57 58 # update stats using all detected numbers from every line 59 { 60 for (i = 1; i <= NF; i++) { 61 s = $i 62 if (s == "" || s ~ / +/ || s ~ /[^0-9.-]/) { 63 continue 64 } 65 66 v = s + 0 67 if (s !~ /\./ || s ~ /\.0*$/) { ints++ } 68 69 count++ 70 min = min < v ? min : v 71 max = max > v ? max : v 72 sum += v 73 prod *= v 74 lnSum += v <= 0 ? nan : log(v) 75 76 if (v > 0) { pos++ } 77 else if (v < 0) { neg++ } 78 else if (v == 0) { zero++ } 79 80 # advance welford`s algorithm 81 d1 = v - mean 82 mean += d1 / count 83 d2 = v - mean 84 meanSq += d1 * d2 85 } 86 } 87 88 # report final numeric stats 89 END { 90 #sum = mean * count 91 92 # separate name-value pairs using tabs, and prepare a 93 # pipeable command which ignores all-zero decimals 94 OFS = "\t" 95 96 # pipe all output lines into the special command, even 97 # when it is seemingly unneeded due to integer values, 98 # since not doing so can scramble the final order of 99 # the output lines 100 print "numeric", count 101 print "min", sprintf("%f", min) 102 print "max", sprintf("%f", max) 103 print "sum", sprintf("%f", sum) 104 print "mean", sprintf("%f", mean) 105 if (lnSum != -inf && count > 0) { 106 geomean = exp(lnSum / count) 107 print "geomean", sprintf("%f", geomean) 108 } else { 109 print "geomean", "" 110 } 111 if (count > 0) { 112 sd = sqrt(meanSq / count) 113 print "sd", sprintf("%f", sd) 114 #print "product", prod 115 print "product", sprintf("%g", prod) 116 } else { 117 print "sd", "" 118 print "product", "" 119 } 120 print "integer", ints 121 print "positive", pos 122 print "zero", zero 123 print "negative", neg 124 } 125 ' "$@" | sed 's-\\.00*$--'