td - Manipulate table data
This document describes version 0.092 of main (from Perl distribution App-td), released on 2019-01-16.
Usage:
% 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).
hos
key
value
aos
elem
aoaos
aohos
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.
[status, message, content, meta]
content
Perinci::CmdLine
meta
td
First you might want to use the info action to see if the input is a table data:
info
% 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 # return the column names only % lcpan related-mods Perinci::CmdLine | td colnames # 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 # select all columns but some % osnames -l --json | td select '*' -e value -e description # only show first 5 rows % osnames -l --json | td head -n5 # show all but the last 5 rows % osnames -l --json | td head -n -5 # only show last 5 rows % osnames -l --json | td tail -n5 # show rows from the row 5 onwards % osnames -l --json | td tail -n +5 # 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
Use this to list all the available actions:
% td actions % td actions -l ;# show details
* marks required options.
*
Action to perform on input table.
Valid values:
["actions","avg","avg-row","colcount","colcount-row","colnames","colnames-row","head","info","rowcount","rowcount-row","rownum-col","select","sort","sum","sum-row","tail","wc","wc-row"]
Arguments (JSON-encoded).
See --argv.
--argv
Arguments.
Default value:
[]
Can be specified multiple times.
Choose output format, e.g. json, text.
undef
Set output format to json.
When outputing as JSON, strip result envelope.
0
By default, when outputing as JSON, the full enveloped result is returned, e.g.:
[200,"OK",[1,2,3],{"func.extra"=>4}]
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:
[1,2,3]
See --exclude-column.
--exclude-column
Display help message and exit.
Display program's version and exit.
The script comes with a companion 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 modules using cpanm-shcompgen which can activate shell completion for scripts immediately.
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).
For fish and zsh, install shcompgen as described above.
Please visit the project's homepage at https://metacpan.org/release/App-td.
Source repository is at https://github.com/perlancar/perl-App-td.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=App-td
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 <perlancar@cpan.org>
This software is copyright (c) 2019, 2017, 2016, 2015 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install App::td, copy and paste the appropriate command in to your terminal.
cpanm
cpanm App::td
CPAN shell
perl -MCPAN -e shell install App::td
For more information on module installation, please visit the detailed CPAN module installation guide.