blob: 926678965e497847f2afe30ea1652068bb1752c2 (
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
38
39
40
41
42
43
|
#!/usr/bin/awk -f
### sum1.awk, print column sums
# input: rows of numbers
# output: sum of each column
# missing entries are treated as zeros
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
}
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, sum[i])
printf((i < nf_max) ? OFS : ORS)
}
}
|