#!/usr/bin/awk -f ### sum3.awk, print sums of numeric columns # input: rows of integers and strings # output: sums of numeric columns # assumes every line has same layout function isnum(n) { return n ~ /^[+-]?[0-9]+$/ } BEGIN { OFS = FS } NR == 1 { nfld = NF for (i=1; i<=NF; i++) numcol[i] = isnum($i) } { for (i=1; i<=NF; i++) if (numcol[i]) sum[i] += $i } END { for (i=1; i<=nfld; i++) { printf(numcol[i] ? sum[i] : "--") printf(i < nfld ? OFS : ORS) } }