-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtable.awk
More file actions
49 lines (46 loc) · 1.11 KB
/
table.awk
File metadata and controls
49 lines (46 loc) · 1.11 KB
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
44
45
46
47
48
49
# Draws boxes around fields to make a pretty-printed table
#
# Use with, eg: cat foo.txt | mawk -F" " -f table.awk
# To take a tab-delimited file and print it with a table around the fields
#
# Given input like:
#
# android 1 10
# blackberry 2 100
# iphone 3 1000
#
# Transforms it into:
# +------------+---+------+
# | android | 1 | 10 |
# | blackberry | 2 | 100 |
# | iphone | 3 | 1000 |
# +------------+---+------+
{
if (!match($1, "^-+$")) {
count += 1
line[count] = $0
for (i = 1; i <= NF; ++i) {
if (length($i) > field_width[i])
field_width[i] = length($i)
}
}
}
function separator_line ( widths ) {
dashes = "--------------------------------------------------------------------------------------"
printf("+")
for (column in widths) {
printf("%-." field_width[column] + 2 "s+", dashes)
}
printf("\n")
}
END {
separator_line(field_width)
for (i = 1; i <= count; ++i) {
$0 = line[i]
printf("|")
for (column in field_width)
printf(" %-" field_width[column] + 1 "s|", $column)
printf("\n")
}
separator_line(field_width)
}