File: sn.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2020-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 # 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 case "$1" in 33 -h|--h|-help|--help) 34 awk '/^# +sn /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 35 exit 0 36 ;; 37 esac 38 39 awk ' 40 # initialize stats 41 BEGIN { 42 inf = "+inf" + 0 43 44 min = inf 45 max = -inf 46 count = 0 47 sum = 0 48 mean = 0 49 prod = 1 50 51 ints = 0 52 pos = 0 53 zero = 0 54 neg = 0 55 } 56 57 # update stats using all detected numbers from every line 58 { 59 for (i = 1; i <= NF; i++) { 60 s = $i 61 if (s == "" || s ~ / +/ || s ~ /[^0-9.-]/) { 62 continue 63 } 64 65 v = s + 0 66 if (s !~ /\./ || s ~ /\.0*$/) { ints++ } 67 68 count++ 69 min = min < v ? min : v 70 max = max > v ? max : v 71 sum += v 72 prod *= v 73 lnSum += v <= 0 ? -inf : log(v) 74 75 if (v > 0) { pos++ } 76 else if (v < 0) { neg++ } 77 else if (v == 0) { zero++ } 78 79 # advance welford`s algorithm 80 d1 = v - mean 81 mean += d1 / count 82 d2 = v - mean 83 meanSq += d1 * d2 84 } 85 } 86 87 # report final numeric stats 88 END { 89 #sum = mean * count 90 if (count == 0) lnSum = -inf 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*$--'