summaryrefslogtreecommitdiff
path: root/sum4.awk
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sum4.awk28
1 files changed, 19 insertions, 9 deletions
diff --git a/sum4.awk b/sum4.awk
index 6a06455..aa3f044 100644
--- a/sum4.awk
+++ b/sum4.awk
@@ -4,6 +4,7 @@
# input: rows of integers and strings
# output: sums of numeric columns
+
function isnum(n) {
sign = "[+-]?"
decimal = "[0-9]+[.]?[0-9]*"
@@ -13,24 +14,33 @@ function isnum(n) {
return n ~ number
}
-NR==1 {
+
+BEGIN { OFS = FS }
+
+NR == 1 {
nf_max = NF
- for (i=1; i<=NF; i++) {
- (!isnum($i)) ? header[i] = $i : header[i] = "col" i
- }
+ for (i=1; i<=NF; i++)
+ isnum($i) ? header[i] = "col" i : header[i] = $i
}
{
+ if (NF > nf_max)
+ nf_max = NF
for (i=1; i<=NF; i++) {
- sum[i] += $i
- count[i]++
+ if ($i == header[i])
+ continue
+ if (isnum($i)) {
+ count[i]++
+ sum[i] += $i
+ }
}
}
END {
for (i=1; i<=nf_max; i++) {
- if (header[i])
- printf("%s:" OFS, header[i])
- printf(OFMT ORS, sum[i])
+ printf((header[i]) ? header[i] OFS : OFS)
+ printf((count[i]) ? count[i] OFS sum[i] : OFS)
+ printf(ORS)
}
}
+