2

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
  • 1
    Instead of echo try printf – Romeo Ninov Jan 23 at 11:53
  • 2
    The questioner is clearly using a shell where -e is implied. We have a whole family of duplicate Q&As about that, starting from unix.stackexchange.com/questions/65803 . But that is not the focus of this question, which is about the column command. Imagine that the input of the column command is whatever is necessary to yield TAB-delimited fields within LF-delimited records, some of which are empty. – JdeBP Jan 23 at 12:14
5

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'
  • Thanks for the sed suggestion! I'm using Mac OS X so don't have GNU column or GNU sed, so this is what ended up working for me: printf 'a\tb\tc\nd\t\tf\n' | sed -$'s/\t\t/\t-\t/g' | column -t -s $'\t' – tekumara Jan 24 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.