-
Notifications
You must be signed in to change notification settings - Fork 5
/
tsvfmt
executable file
·68 lines (63 loc) · 1.74 KB
/
tsvfmt
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/sh
#tsvfmt - TSV Formatter
#Author: Donald L. Merand
#----------
#Takes a TSV file, and outputs a text-only representation of the table,
#+ justified to the max width of each column. Basically, you use it when
#+ you want to "preview" a TSV file, or print it prettily.
#----------
#Accepts standard input, or piped/redirected files
#----------
#set this to whatever you want to show up between columns
field_separator="|"
tmp_filename=tsv_${RANDOM}_tmp.txt
tmp_widths=tsv_${RANDOM}_widths.txt
#take file input, or standard input, and redirect to a temporary file
#also, convert mac line-endings to UNIX ones
perl -p -e 's/(:?\r\n?|\n)/\n/g' > "$tmp_filename"
#now we're going to extract max record widths from the file
#send the contents to awk
cat "$tmp_filename" |
awk '
BEGIN {
FS="\t"
max_nf = 0
}
{
for (i=1; i<=NF; i++) {
#set the max-length to this field width if it is the biggest
if (max_length[i] < length($i)) { max_length[i] = length($i) }
}
if (max_nf < NF) { max_nf = NF }
}
END {
for (i=1; i<=max_nf; i++) {
printf("%s\t", max_length[i])
}
printf("\n")
}
' > "$tmp_widths" #store widths in a TSV temp file
#now start over by sending our temp file to awk. THIS time we have a widths
#+file to read which gives us the maximum width for each column
cat "$tmp_filename" |
awk -v field_sep="$field_separator" '
BEGIN {
FS="\t"
#read the max width of each column
getline w < "'"$tmp_widths"'"
#split widths into an array
split(w, widths, "\t")
#get the max number of fields
max_nf = 0
for (i in widths) { max_nf++ }
}
{
printf("%s", field_sep)
for (i=1; i<max_nf; i++) {
printf("%-*s%s", widths[i], $i, field_sep)
}
printf("\n")
}
'
#now we're done. remove temp files
rm "$tmp_filename" "$tmp_widths"