#!/bin/sh # The MIT License (MIT) # # Copyright © 2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # sn [filepaths...] # # Summarize Numbers does what it says, using all numbers detected from the # input(s). When given no filepaths, it reads from standard input. case "$1" in -h|--h|-help|--help) awk '/^# +sn /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac [ "$1" = "--" ] && shift # show all non-existing files given failed=0 for arg in "$@"; do if [ "${arg}" = "-" ]; then continue fi if [ ! -e "${arg}" ]; then printf "no file named \"%s\"\n" "${arg}" > /dev/stderr failed=1 fi done if [ "${failed}" -gt 0 ]; then exit 2 fi awk ' BEGIN { numeric = ints = pos = zero = neg = 0 inf = "+inf" + 0 min = inf max = -inf sum = 0 mean = 0 prod = 1 } # update stats using all detected numbers from every line { for (i = 1; i <= NF; i++) { s = $i if (s == "" || s ~ / +/ || s ~ /[^0-9\.-]/) { continue } v = s + 0 numeric++ ints += v % 1 == 0 if (v > 0) pos++ else if (v < 0) neg++ else if (v == 0) zero++ min = min < v ? min : v max = max > v ? max : v sum += v prod *= v lnSum += v <= 0 ? -inf : log(v) # advance welford`s algorithm d1 = v - mean mean += d1 / numeric d2 = v - mean meanSq += d1 * d2 } } END { sum = mean * numeric if (numeric == 0) lnSum = -inf # separate name-value pairs using tabs, and prepare a # pipeable command which ignores all-zero decimals OFS = "\t" print "numeric", numeric if (numeric > 0) { print "min", sprintf("%f", min) print "max", sprintf("%f", max) print "sum", sprintf("%f", sum) print "mean", sprintf("%f", mean) print "geomean", (zero == 0 && neg == 0) ? sprintf("%f", exp(lnSum / numeric)) : "" print "sd", sprintf("%f", sqrt(meanSq / numeric)) print "product", sprintf("%g", prod) } else { print "min", "" print "max", "" print "sum", "" print "mean", "" print "geomean", "" print "sd", "" print "product", "" } print "integer", ints print "positive", pos print "zero", zero print "negative", neg } ' "$@" | sed -E 's-([0-9]+)\.0+$-\1-g; s-([0-9]+\.[0-9]*[1-9])0+$-\1-g'