1# table - simple table formatter
2
3BEGIN {
4    FS = "\t"; blanks = sprintf("%100s", " ")
5    number = "^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)$"
6}
7
8{   row[NR] = $0
9    for (i = 1; i <= NF; i++) {
10        if ($i ~ number)
11            nwid[i] = max(nwid[i], length($i))
12        wid[i] = max(wid[i], length($i))
13    }
14}
15
16END {
17    for (r = 1; r <= NR; r++) {
18        n = split(row[r], d)
19        for (i = 1; i <= n; i++) {
20            sep = (i < n) ? "   " : "\n"
21            if (d[i] ~ number)
22                printf("%" wid[i] "s%s", numjust(i,d[i]), sep)
23            else
24                printf("%-" wid[i] "s%s", d[i], sep)
25        }
26    }
27}
28
29function max(x, y) { return (x > y) ? x : y }
30
31function numjust(n, s) {   # position s in field n
32    return s substr(blanks, 1, int((wid[n]-nwid[n])/2))
33}
34