td - Manipulate table data


This document describes version 0.08 of main (from Perl distribution App-td), released on 2016-01-01.



 % td [options] <action> [argv] ...


td receives table data from standard input and performs an action on it. It has functionality similar to some Unix commands like head, tail, wc, cut, sort except that it operates on table rows/columns instead of lines/characters. This is convenient to use with CLI scripts that output table data.

A table data is JSON-encoded data in the form of either: hos (hash of scalars, which is viewed as a two-column table where the columns are key and value), aos (array of scalars, which is viewed as a 1-column array where the column is elem), aoaos (array of arrays of scalars), or aohos (array of hashes of scalars).

The input can also be an enveloped table data, where the envelope is an array: [status, message, content, meta] and content is the actual table data. This kind of data is produced by Perinci::CmdLine-based scripts and can contain more detailed table specification in the meta hash, which td can parse.

First you might want to use the info action to see if the input is a table data:

 % osnames -l --json | td info

If input is not valid JSON, a JSON parse error will be displayed. If input is valid JSON but not a table data, another error will be displayed. Otherwise, information about the table will be displayed (form, number of columns, column names, number of rows, and so on).

Next, you can use these actions:

 # count number of rows (equivalent to "wc -l" Unix command)
 % osnames -l --json | td rowcount
 # append a row containing rowcount
 % osnames -l --json | td rowcount-row
 # append a row containing column names
 % lcpan related-mods Perinci::CmdLine | td colnames-row
 # count number of columns
 % osnames -l --json | td colcount
 # select some columns
 % osnames -l --json | td select value description
 # only show first 5 rows
 % osnames -l --json | td head -n5
 # only show last 5 rows
 % osnames -l --json | td tail -n5
 # sort by column(s) (add "-" prefix to for descending order)
 % osnames -l --json | td sort value tags
 % osnames -l --json | td sort -- -value
 # return sum of all numeric columns
 % list-files -l --json | td sum
 # append a sum row
 % list-files -l --json | td sum-row
 # return average of all numeric columns
 % list-files -l --json | td avg
 # append an average row
 % list-files -l --json | td avg-row
 # add a row number column (1, 2, 3, ...)
 % list-files -l --json | td rownum-col


* marks required options.

Main options


Action to perform on input table.

Valid values:


Arguments (JSON-encoded).

See --argv.



Default value:


Can be specified multiple times.

--lines=i, -n

Output options


Choose output format, e.g. json, text.

Default value:


Set output format to json.


When outputing as JSON, strip result envelope.

Default value:


By default, when outputing as JSON, the full enveloped result is returned, e.g.:


The reason is so you can get the status (1st element), status message (2nd element) as well as result metadata/extra result (4th element) instead of just the result (3rd element). However, sometimes you want just the result, e.g. when you want to pipe the result for more post-processing. In this case you can use `--naked-res` so you just get:


Other options

--help, -h, -?

Display help message and exit.

--version, -v

Display program's version and exit.


The distribution comes with a shell completer script (_td) for this script.


To activate bash completion for this script, put:

 complete -C _td td

in your bash startup (e.g. ~/.bashrc). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is recommended, however, that you install shcompgen which allows you to activate completion scripts for several kinds of scripts on multiple shells. Some CPAN distributions (those that are built with Dist::Zilla::Plugin::GenShellCompletion) will even automatically enable shell completion for their included scripts (using shcompgen) at installation time, so you can immadiately have tab completion.


To activate tcsh completion for this script, put:

 complete _td 'p/*/`td`/'

in your tcsh startup (e.g. ~/.tcshrc). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is also recommended to install shcompgen (see above).

other shells

For fish and zsh, install shcompgen as described above.


Please visit the project's homepage at


Source repository is at


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


perlancar <>


This software is copyright (c) 2017 by

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.