summaryrefslogtreecommitdiff
path: root/sum_col.awk
diff options
context:
space:
mode:
authorwukong <wukong@longaeva>2026-05-03 13:10:31 -0700
committerwukong <wukong@longaeva>2026-05-03 13:10:31 -0700
commitb60f09401a15721f626bcb7c852f68b82fe39c1c (patch)
tree0fa5ed72ec96c8b0cc83699a0497761c9ec0eed7 /sum_col.awk
parente3f9ccd5303c516d2e1c48112d8b52609ce4ad5e (diff)
replaced sum[1-4].awk with serial and columnar versions;
minor merge updates between fir_window scripts;
Diffstat (limited to 'sum_col.awk')
-rw-r--r--sum_col.awk47
1 files changed, 47 insertions, 0 deletions
diff --git a/sum_col.awk b/sum_col.awk
new file mode 100644
index 0000000..f08ec06
--- /dev/null
+++ b/sum_col.awk
@@ -0,0 +1,47 @@
+#!/usr/bin/awk -f
+
+### sum_col.awk
+# sum columns of numerical data
+# input: delimited data as text
+# output: list of univariate summary stats
+
+
+BEGIN {
+ OFS = FS
+ sign = "[+-]?"
+ decimal = "[0-9]+[.]?[0-9]*"
+ fraction = "[.][0-9]+"
+ exponent = "([Ee]" sign "[0-9]+)?"
+ number = "^" sign "(" decimal "|" fraction ")" exponent "$"
+}
+
+
+# read column headers
+NR == 1 {
+ for (n=1; n<=NF; n++)
+ ($n ~ number) ? header[n] = "col" n : header[n] = $n
+}
+
+
+# iterate over columns
+NF > 0 {
+ (NF > nf_max) ? nf_max = NF : nf_max = nf_max
+ for (n=1; n <= NF; n++) {
+ if ($n ~ number) {
+ count[n] += 1
+ sum[n] += $n
+ }
+ }
+}
+
+
+END {
+ print "col", "sum", "count"
+ for (n=1; n<=nf_max; n++) {
+ if (header[n])
+ print header[n], sum[n], count[n]
+ else
+ print "col" n, sum[n], count[n]
+ }
+}
+