Convert tab separated values to ASCII table

Solution 1:

How can I convert tab separated values to an ASCII table?

I use Text Tables Generator for this kind of task.

I pasted your data on that page and it created the following table:

+------+------+------+------+------------+
| a    | b    | c    | d    | cat        |
+------+------+------+------+------------+
| NULL | NULL | NULL | NULL | NULL       |
+------+------+------+------+------------+
| NULL | NULL | NULL | d    | d          |
+------+------+------+------+------------+
| NULL | NULL | c    | NULL | c          |
+------+------+------+------+------------+
| NULL | NULL | c    | d    | c; d       |
+------+------+------+------+------------+
| NULL | b    | NULL | NULL | b          |
+------+------+------+------+------------+
| NULL | b    | NULL | d    | b; d       |
+------+------+------+------+------------+
| NULL | b    | c    | NULL | b; c       |
+------+------+------+------+------------+
| NULL | b    | c    | d    | b; c; d    |
+------+------+------+------+------------+
| a    | NULL | NULL | NULL | a          |
+------+------+------+------+------------+
| a    | NULL | NULL | d    | a; d       |
+------+------+------+------+------------+
| a    | NULL | c    | NULL | a; c       |
+------+------+------+------+------------+
| a    | NULL | c    | d    | a; c; d    |
+------+------+------+------+------------+
| a    | b    | NULL | NULL | a; b       |
+------+------+------+------+------------+
| a    | b    | NULL | d    | a; b; d    |
+------+------+------+------+------------+
| a    | b    | c    | NULL | a; b; c    |
+------+------+------+------+------------+
| a    | b    | c    | d    | a; b; c; d |
+------+------+------+------+------------+

You can then copy this output (the generator has done most of the hard work), paste into notepad++ and clean up as appropriate.

Solution 2:

If you need a command-line solution, you can also use pandoc with the pandoc-placetable filter.

Place your table in foo.txt and execute:

pandoc-placetable --file=foo.txt --delimiter="\t" --header | pandoc -f json -t markdown-simple_tables-multiline_tables -o output.md

Which results in the following output.md:

| a    | b    | c    | d    | cat        |
|------|------|------|------|------------|
| NULL | NULL | NULL | NULL | NULL       |
| NULL | NULL | NULL | d    | d          |
| NULL | NULL | c    | NULL | c          |
| NULL | NULL | c    | d    | c; d       |
| NULL | b    | NULL | NULL | b          |
| NULL | b    | NULL | d    | b; d       |
| NULL | b    | c    | NULL | b; c       |
| NULL | b    | c    | d    | b; c; d    |
| a    | NULL | NULL | NULL | a          |
| a    | NULL | NULL | d    | a; d       |
| a    | NULL | c    | NULL | a; c       |
| a    | NULL | c    | d    | a; c; d    |
| a    | b    | NULL | NULL | a; b       |
| a    | b    | NULL | d    | a; b; d    |
| a    | b    | c    | NULL | a; b; c    |
| a    | b    | c    | d    | a; b; c; d |

To read from STDIN, leave out the --file argument. To print to STDOUT, leave out the -o argument.

Solution 3:

ruslan’s idea of using the Unix/Linux column command is a good one, but the command line given in their answer doesn’t quite work. First of all, column doesn’t recognize \t (or \\t) on the command line as a tab.  If you have bash, you can do

column -t -s$'\t' foo.txt

Otherwise, you can do

column -t -s"$(printf '\t')" foo.txt

But even that doesn’t answer the question.  You can get the vertical bars by doing

column -t -s$'\t' -o' | ' foo.txt

which produces output like

a    | b    | c    | d    | cat
NULL | NULL | NULL | NULL | NULL
NULL | NULL | NULL | d    | d
NULL | NULL | c    | NULL | c
NULL | NULL | c    | d    | c; d
NULL | b    | NULL | NULL | b
NULL | b    | NULL | d    | b; d
NULL | b    | c    | NULL | b; c
NULL | b    | c    | d    | b; c; d
a    | NULL | NULL | NULL | a
a    | NULL | NULL | d    | a; d
a    | NULL | c    | NULL | a; c
a    | NULL | c    | d    | a; c; d
a    | b    | NULL | NULL | a; b
a    | b    | NULL | d    | a; b; d
a    | b    | c    | NULL | a; b; c
a    | b    | c    | d    | a; b; c; d

Adding the dash line after the header manually isn’t so tedious.


If you don’t have access to a full Unix/Linux system, you can use Cygwin or one of the other Unix-likes for this.