banner

For a list of BASHing data 2 blog posts see the index page.    RSS


Pretty-printing a table in the terminal

Here's a table, "trips.csv", which I can pretty-print using the column utility:

Trip,Start Date,End Date,km travelled
A1,2024-05-06,2024-05-19,42
A2,2024-06,2024-08-13,291
A3,2024-07-01,2024-09,1863
B1,2024-08-14,2024-10-06,317

pp1

column -t -s "," -R 2,3,4 trips.csv

The -t option lets column find the number of columns separated by a comma (-s ","), and the -R 2,3,4 option tells column to right-justify columns 2, 3 and 4.

Well, it's neat, but it's not very pretty. You can get a bit closer to "pretty" with three little-known CLI programs: rows, csvlook and PrettyTable.


rows is a Python program written by Álvaro Justen and it's available in the Debian repositories, so dependencies are taken care of during installation. It pretty-prints tables in CSV (comma), CSV (semicolon), TSV and .xlsx format but it needs the file to have an appropriate suffix (not just ".txt"). The command is rows print [file]:

pp2

I like the fact that rows automatically right-justifies, and that header items (only) are de-capitalised and joined with underscores. Another nice feature is that dates in non-ISO 8601 formats are re-formatted as ISO 8601. For example, "29/11/97" becomes "1997-11-29T00:00:00" in the pretty-print.


csvlook is included in the csvkit package, which has many contributing developers. The output is nice and simple and numbers are right-justified:

pp3

You can specify the field delimiter of the input file (see the csvkit documentation), but csvlook can also guess the separator and pretty-print without being told the delimiter, and you don't need a filename suffix ("trips" will pretty-print).

Another nice feature of csvlook is that the -l option adds line numbers to the records in the table (excluding the header):

pp4

PrettyTable is a simple BASH shell script from Jakob Westhoff and is available on GitHub. Put the script in your $PATH and make it executable and it's ready to go. PrettyTable requires tab-separated data and you have to specify the number of columns as a first argument. Once the table is a TSV, pipe it to PrettyTable as shown below. You can add a color name as a second argument to color the header line:

pp5

csvformat -T trips.csv | prettytable 4 red

csvformat is included in the csvkit package in the repositories.


What about wide tables? All three of these utilities can pretty-print tables much wider than your terminal. Just pipe the output to less -S, and move right and left in the pretty-printed table with right-arrow and left-arrow keys. (Use less -RS with PrettyTable to allow color and avoid seeing escape notation.) I'd demonstrate this here but it's not an easy thing to turn into a graphic!

From the less manpage:
 
-S or --chop-long-lines
Causes lines longer than the screen width to be chopped (truncated) rather than wrapped. That is, the portion of a long line that does not fit in the screen width is not displayed until you press RIGHT-ARROW. The default is to wrap long lines; that is, display the remainder on the next line.


Next post:
2024-11-15   Merging tables with (some) shared fields


Last update: 2024-11-08
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License