summaryrefslogtreecommitdiff
path: root/sum2.awk
blob: 694c0477351b7fc01acc3807a35ed2d60778c30b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/awk -f

### sum2.awk, print column sums
# check that each line has the same number of fields as line one

BEGIN { OFS = FS }

NR == 1 { nf_max = NF }

{
    for (i=1; i<=NF; i++)
        sum[i] += $i
    if (NF != nf_max)
        print "line " NR " has " NF " entries, not " nf_max
}

END {
    for (i=1; i<=nf_max; i++)
        printf(OFMT "%s", sum[i], i < nf_max ? OFS : ORS)
}