-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcat
executable file
·81 lines (76 loc) · 3.14 KB
/
tcat
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
69
70
71
72
73
74
75
76
77
78
79
80
81
#! /usr/bin/awk -f
# tcat - tabulate output by aligning fields in text file
# Copyright (C) 2019 Erik Auerswald <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Take input text with fields separated by <space> and output them padded
# with <space> characters to align fields. Output fields separated by <space>.
#
# This is an alternative implementation of rsc.io/tcat which I stumbled
# upon reading the https://research.swtch.com/vgo-repro blog post.
# rsc.io/tcat seems to output two <space>s between fields, while this
# script outputs one <space> by default.
#
# Awk's FS is used to split input records (usually lines) into fields,
# Awk's OFS is used to separate output fields. FS and OFS can be controlled
# using command line operands.
#
# Setting OFS to two <spaces> (-v OFS=' ') results in two <space>s between
# output fields as in the rsc.io/tcat implementation.
#
# The command "column -t" of BSD Unix origin, available in Debian GNU/Linux
# as part of the bsdmainutils package, produces similar output to rsc.io/tcat.
#
# By default, "column" will ignore empty input lines, and omit them from output,
# while this script will keep them. The "-e" option to column keeps empty lines.
#
# By default, "column" will treat multiple adjacent delimiters as delimiting
# exactly two fields, while this script will treat multiple adjacent delimiters
# as delimiting multiple fields, if the field separator is set to a non-default
# value (using option "-F <value>" or "-v FS=<value>"). To allow merging of
# adjacent delimiters, a regular expression can be used (e.g., -F '\t+').
# The "-n" option to Debian's "column" fork inhibits adjacent delimiter merging.
#
# For input files where one or more TAB characters separate fields, the
# following invocations of "column" respectivly "tcat" produce similar output:
#
# column -e -t -s '^V^I'
# tcat -F '\t+' -v OFS=' '
#
# Examples:
# $ printf 'a b c\naa bbbb\naaa b c\na bbb c\n' | tcat
# $ tcat -F ':' -v OFS=' ' /etc/passwd
# $ getent group | cut -d: -f1,4 | tcat -F: -v OFS=': '
# $ sed '/^#/d' $HOME/.wget-hsts | tcat -F'\t' -v OFS=' '
# $ apt-cache search -n erlang | tcat -F ' - ' -v OFS=' ' | cut -c-$COLUMNS
function max(a, b)
{
return a > b ? a : b
}
{
line[NR] = $0
for (f = 1; f <= NF; f++) {
max_field_width[f] = max(length($f), max_field_width[f])
}
}
END {
for (l = 1; l <= NR; l++) {
num_fields = split(line[l], tmp_array)
for (f = 1; f <= num_fields; f++) {
if (f > 1) printf("%s", OFS)
printf("%-" max_field_width[f] "s", tmp_array[f])
}
printf("%s", ORS)
}
}