NAME
td - Manipulate table data
VERSION
This document describes version 0.08 of main (from Perl distribution App-td), released on 2016-01-01.
SYNOPSIS
Usage:
% td [options] <action> [argv] ...
DESCRIPTION
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
OPTIONS
*
marks required options.
Main options
- --action=s*
-
Action to perform on input table.
Valid values:
["avg","avg-row","colcount","colcount-row","colnames-row","head","info","rowcount","rowcount-row","rownum-col","select","sort","sum","sum-row","tail","wc","wc-row"]
- --argv-json=s
-
Arguments (JSON-encoded).
See
--argv
. - --argv=s@
-
Arguments.
Default value:
[]
Can be specified multiple times.
- --lines=i, -n
Output options
- --format=s
-
Choose output format, e.g. json, text.
Default value:
undef
- --json
-
Set output format to json.
- --naked-res
-
When outputing as JSON, strip result envelope.
Default value:
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]
Other options
COMPLETION
The distribution comes with a shell completer script (_td) for this script.
bash
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.
tcsh
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.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/App-td.
SOURCE
Source repository is at https://github.com/perlancar/perl-App-td.
BUGS
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.
AUTHOR
perlancar <perlancar@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2017 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.