Convert tab separated values to ASCII table

asciiconversioncsvtext formatting

What is the most efficient way to convert tab separated data such as this:

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

Something close to this:

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

Currently I use Notepad++ as follows:

  1. Convert tabs to spaces
  2. Align the data manually
  3. Use column mode to insert the pipes

The second step is the most tedious one and I would rather have at least this part automated.

Note: I use a browser when working and sometimes I have a text editor open alongside. The efficient solution is the one that requires least amount of effort. I can use:

  • Notepad++
  • Generic text editor with regexp find/replace support
  • JavaScript typed inside browser console
  • Online web service
  • PHP on command line (php -a)

Best Answer

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.

Related Question