#!/usr/bin/awk -f ### sum2.awk, print column sums # check that each line has the same number of fields as line one BEGIN { OFS = FS } NR == 1 { for (i=1; i<=NF; i++) isnum($i) ? header[i] = "col" i : header[i] = $i } NF > 0 { (NF > nf_max) ? nf_max = NF : nf_max = nf_max for (i=1; i<=NF; i++) sum[i] += $i } END { for (i=1; i<=nf_max; i++) printf(OFMT "%s", sum[i], i < nf_max ? OFS : ORS) }