#!/usr/bin/awk -f ### sum4.awk, print sums of numeric columns # input: rows of integers and strings # output: sums of numeric columns function isnum(n) { sign = "[+-]?" decimal = "[0-9]+[.]?[0-9]*" fraction = "[.][0-9]+" exponent = "([Ee]" sign "[0-9]+)?" number = "^" sign "(" decimal "|" fraction ")" exponent "$" return n ~ number } NR==1 { nf_max = NF for (i=1; i<=NF; i++) { if (!isnum($i)) header[i] = $i } } { for (i=1; i<=NF; i++) { sum[i] += $i count[i]++ } } END { for (i=1; i<=nf_max; i++) { if (header[i]) printf("%s: \t", header[i]) printf("%g\n", sum[i]) printf(i < nf_max ? "" : "\n") } }