summaryrefslogtreecommitdiff
path: root/sum4.awk
blob: c592409a0641f4ba71a9a04c69aeca93de1106be (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
32
33
34
35
36
37
### 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 {
    nfldz = 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<=nfldz; i++) {
        if (header[i])
            printf("%s: \t", header[i])
        #if (numcol[i])
        printf("%g\n", sum[i])
        printf(i < nfldz ? "" : "\n")
    }
}