summaryrefslogtreecommitdiff
path: root/sum3.awk
blob: b4100a78284dc2160af6d9ec0fd2ae562f8ca977 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
### sum3.awk, print sums of numeric columns
# input:    rows of integers and strings
# output:   sums of numeric columns
#   assumes every line has same layout

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++) {
        if (numcol[i])
            printf("%g", sum[i])
        else
            printf("--")
        printf(i < nfld ? "  " : "\n")
    }
}

function isnum(n) {
    return n ~ /^[+-]?[0-9]+$/
}