打印带有连续分隔符的空列的表

tek*_*ara 3 shell columns text-formatting

我通常用于column将输入转换为表格,例如:

$ echo 'a\tb\tc\nd\te\tf' | column -t -s $'\t'
a  b  c
d  e  f
Run Code Online (Sandbox Code Playgroud)

但是它会折叠空列,例如:

$ echo 'a\tb\tc\nd\t\tf' | column -t -s $'\t'
a  b  c
d  f
Run Code Online (Sandbox Code Playgroud)

当有连续的​​分隔符时,而不是打印一个空列。这就是我想要的,使用column或以其他方式:

a  b  c
d     f
Run Code Online (Sandbox Code Playgroud)

pLu*_*umo 5

如果您使用GNU column

-n
默认情况下,column 命令在使用 -t 选项时会将多个相邻的分隔符合并为一个分隔符;此选项禁用该行为。此选项是 Debian GNU/Linux 扩展。

printf 'a\tb\tc\nd\t\tf\n'  | column -t -n -s $'\t'
Run Code Online (Sandbox Code Playgroud)

输出:

a  b  c
d     f
Run Code Online (Sandbox Code Playgroud)

如果GNU column不可用,您可以使用 sed-在选项卡之间添加一个空格(或其他东西,例如 a ):

printf 'a\tb\tc\nd\t\tf\n'  | sed -e ':loop; s/\t\t/\t-\t/; t loop' | column -t -s $'\t'
Run Code Online (Sandbox Code Playgroud)