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