diff options
Diffstat (limited to 'sum_col.awk')
| -rw-r--r-- | sum_col.awk | 47 |
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] + } +} + |
