blob: d9de4efdeefb1361a7eef97a2e4e4183835295ff (
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
|
#!/usr/bin/awk -f
### sum.awk
# calculate sum of serialized input
BEGIN {
OFS = FS
sign = "[+-]?"
decimal = "[0-9]+[.]?[0-9]*"
fraction = "[.][0-9]+"
exponent = "([Ee]" sign "[0-9]+)?"
number = "^" sign "(" decimal "|" fraction ")" exponent "$"
}
# non-empty rows (records)
NF > 0 {
# iterate over fields
for (n=1; n<=NF; n++) {
if ($n ~ number) {
count += 1
sum += $n
}
}
}
END {
print "sum", "count"
print sum, count
}
|