The table formatting utility

Back to the root node

Introduction

Sometimes, when processing various data sets of roughly tabular structure, it is useful to display the data in rows and columns for human review. For handling simple data sets, the column(1) utility was written as a part of 4.3BSD. However, for more complex displays, this tool was insufficient, though there was some feature creep in some linux implementations. On the other hand, the tbl(1) preprocessor in the Troff suite is suitable for typesetting even highly complicated tables in print quality, but a considerable effort needs to be put into defining the table outline and properties. This utility aims in the uncovered space between those two tools, namely, formatting the (usually simple .csv) data into simple columns without much extra work while respecting left- and right-alignment, center, and alignment of decimal numbers so that decimal points are aligned vertically (sorry, no support for exponential numbers). Also, some highlighting capabilities are implemented to aid visual inspection of the data. The output is mainly intended to be displayed on a terminal [or terminal emulator like xterm], preferrably with support for ANSI colors, but also printing on fixed width printers ("line printers") or feeding into a2ps(1) or mpage(1) is possible.

Description

The program works as a filter. The input file(s) can be either supplied as a command line arguments, or piped into the stdin. The output is either directly displayed on the terminal (via stdout), or piped into suitable pager with support for ANSI colors (e. g. less -R). Further processing or hardcopy printing is also possible.

There is also a roff mode which is intended for processing troff documents with a table suitable for the tbl(1) preprocessor. In this case, no formatting information is supplied on the command line, as it is obtained from the table header. Only lines between the first pair of .TS/.TE directives is processed. Also keep in mind that only a subset of the tbl(1) functionality is implemented in table(1).

table(1) is implemented completely in the AWK programming language. GNU AWK is tested, but nawk should work as well, with certain care when invoking the program on the command line. However, caution is advised regarding the output, as no testing using nawk was done as of now.

Download

The version 0.1 of table(1) is available here: table-0.1.tgz. The manual page is available online as well.

The version 0.2 of table(1) is available here: table-0.2.tgz. Ver. 0.2 includes several fixes, preliminary support of multiple tables in Roff mode, and especially a WWW mode, intended to produce HTML tables, invoked by the -W option. Without Roff mode, table(1) tries to produce HTML table suitable for incorporation to a HTML document (i. e. you are still responsible for providing the rest of the page, including the HTML and BODY tags etc). In Roff mode, table(1) produces input for groff -Thtml to produce entire document with possibly multiple tables. The contents of the input file outside the .TS and .TE directives are not touched. Keep in mind that the WWW mode is not extensively tested.

Please note that the HTML is poorly designed and hopelessly broken document format, lacking decent tools for even reasonably formatting simple table elements like decimal numbers or horizontal lines. The CSS does not help here. The output generated by table(1) is a tradeoff between portability, flexibility in general (roff) document preparation, and at least reasonable visual appearance in some of the common browsers. If you do not like the output, use the groff’s internal tbl preprocessor instead and generate a PostScript or PDF document. The HTML output can be improved for specific purposes at the expense of sharply growig complexity, but for general document preparation, the HTML will never be a good target.

WWW mode for Roff documents can be invoked, for example, the following way:

table -WR document.t | groff -ms -ep -Thtml > document.html

Installation

There is no automated installation yet. Put the table script into your path and set its executable bits, if not already. Put the manual page (named table.1) into your manpath, or in /usr/local/man/man1 or other suitable location.

If you want to try to use nawk, update the first line of the to reflect the actual path to the awk interpreter and change the −E option to a standard −f option recognized by all awk implementation. Then, the first line of the script should then resemble this:

#! /usr/bin/nawk -f

However, beware that nawk is not tested at all, and that accidental invocation of table with any of the valid awk options may completely break the software and bring unpredictable results!


Last revision: 2023-03-19