Shell – Print table with empty columns for consecutive delimiters

columnsshelltext formatting

I typically use column to convert input into a table, eg:

$ echo 'a\tb\tc\nd\te\tf' | column -t -s $'\t'
a  b  c
d  e  f

However it collapses empty columns eg:

$ echo 'a\tb\tc\nd\t\tf' | column -t -s $'\t'
a  b  c
d  f

Rather than printing an empty column when there are consecutive delimiters. This is what I would like, using column or otherwise:

a  b  c
d     f

Best Answer

If you use GNU column:

-n
By default, the column command will merge multiple adjacent delimiters into a single delimiter when using the -t option; this option disables that behavior. This option is a Debian GNU/Linux extension.

printf 'a\tb\tc\nd\t\tf\n'  | column -t -n -s $'\t'

Output:

a  b  c
d     f

If GNU column is not available, you can use sed to add a space (or something else, e.g. a -) between the tabs:

printf 'a\tb\tc\nd\t\tf\n'  | sed -e ':loop; s/\t\t/\t-\t/; t loop' | column -t -s $'\t'
Related Question