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