Fsdb::F...ilepivot(3)
π
Fsdb::F...ilepivot(3) β aka dbfilepivot, bestie β¨
okay so this whole page lives in the User Contributed Perl Documentation section of the manual, and honestly? it's a whole vibe. buckle up π
β¨ NAME β¨
dbfilepivot β pivot a table, converting multiple rows into a single wide row π¦
β¨ SYNOPSIS β¨
dbfilepivot [-e empty] -k KeyField -p PivotField [-v ValueField]
β¨ DESCRIPTION β¨
So you take a table and you pivot it, right? that means: multiple rows that all correspond to the same key get smooshed together into one single wide row. it's giving glow-up π
picture a normalized database β data with a schema like (id, attribute, value). valid, correct, very demure. but sometimes you'd way rather see it as (id, attribute1, attribute2). (for example, gnuplot's stacked histograms requires denormalized data, no cap.) Dbfilepivot converts the normalized format into the denormalized (but sometimes useful!) format. here the "id" is the key, the attribute is the "pivot", and the value is, well, the optional "value" π«Ά
an example makes it so much clearer. a gradebook usually looks like:
#fsdb name hw_1 hw_2 hw_3
John 97 98 99
Paul - 80 82
but a properly normalized format would represent it as:
#fsdb name hw score
John 1 97
John 2 98
John 3 99
Paul 2 80
Paul 3 82
this tool converts the second form into the first, when you run it as
dbfilepivot -k name -p hw -v score
or
dbfilepivot --possible-pivots='1 2 3' -k name -p hw -v score
here name is the key column that says which rows belong to the same entity, hw is the pivot column that indicates which column in the output is relevant, and score is the value that says what actually goes in the output. iconic teamwork π
the pivot creates a new column "key_tag1", "key_tag2", etc. for each tag β that's the contents of the pivot field in the input. then it populates those new columns with the contents of the value field in the input. slay β¨
if no value column is specified, then values are either empty or 1.
β οΈ heads up bestie:Dbfilepivotassumes all lines with the same key are adjacent in the input source, likedbmapreduce(1)with the-Soption. to enforce this invariant, by default it requires input be sorted by key. no exceptions, periodt.
there is no requirement that the pivot field be sorted (provided the key field is already sorted). lowkey a relief ngl πΈ
by default, dbfilepivot makes two passes over its data, which means it needs temporary disk space equal to the input size. π with the --possible-pivots option you can specify the pivots yourself, skip that second pass, and avoid the temporary data storage entirely. main character optimization energy.
memory usage is proportional to the number of unique pivot values.
the inverse of this command is dbcolsplittorows π¦
β¨ OPTIONS β¨
-kor--keyKeyField β specify which column is the key for grouping. Required (no default). you literally cannot skip this one π«Ά-por--pivotPivotField β specify which column is the key to indicate which column in the output is relevant. Required (no default).-vor--valueValueField β specify which column is the value in the output. if none is given,1is used for the value.--possible-pivotsPP β specify all possible pivot values as PP, a whitespace-separated list. with this option, data is processed only once (not twice). efficiency queen π-CS or--element-separatorS β specify the separator S used to join the input's key column with its contents. (defaults to a single underscore_.)-eE or--emptyE β give value E as the value for empty (null) records.-Sor--pre-sortedβ assume data is already grouped by key. provided twice, it removes the validation of this assertion. by default, we sort by key.-TTmpDir or--tmpdirTmpDir β where to put tmp files. also uses environment variableTMPDIR, if-Tis not specified. default is/tmp.
this module also supports the standard fsdb options, babes:
-dβ enable debugging output.-ior--inputInputSource β read from InputSource, typically a file name, or"-"for standard input, or (if in Perl) aIO::Handle,Fsdb::IOorFsdb::BoundedQueueobjects.-oor--outputOutputDestination β write to OutputDestination, typically a file name, or"-"for standard output, or (if in Perl) aIO::Handle,Fsdb::IOorFsdb::BoundedQueueobjects.--autorunor--noautorunβ by default, programs process automatically, butFsdb::Filterobjects in Perl do not run until you invoke therun()method. the"--(no)autorun"option controls that behavior within Perl.--helpβ show help.--manβ show full manual.
β¨ SAMPLE USAGE β¨ π
Input:
#fsdb name hw score
John 1 97
John 2 98
John 3 99
Paul 2 80
Paul 3 82
Command:
cat data.fsdb | dbfilepivot -k name -p hw -v score
Output:
#fsdb name hw_1 hw_2 hw_3
John 97 98 99
Paul - 80 82
# | dbfilepivot -k name -p hw -v score
look at that transformation. literally ate. π₯
β¨ SEE ALSO β¨
Fsdb(3). dbcolmerge(1). dbcolsplittorows(1). dbcolsplittocols(1). go read them, they're all friends π§
β¨ CLASS FUNCTIONS β¨ (for the Perl girlies π)
new
$filter = new Fsdb::Filter::dbfilepivot(@arguments);
create a new dbfilepivot object, taking command-line arguments.
set_defaults
$filter->set_defaults();
internal: set up defaults.
parse_options
$filter->parse_options(@ARGV);
internal: parse command-line arguments.
_find_possible_pivots
$filter->_find_possible_pivots();
internal: scan input data to find all possible pivot values.
returns npivots, pivots_aref.
_given_possible_pivots
$filter->_given_possible_pivots();
internal: parse option of possible pivots.
returns npivots, pivots_href.
setup
$filter->setup();
internal: setup, parse headers.
run
$filter->run();
internal: run over each rows.
β¨ AUTHOR and COPYRIGHT β¨
copyright (C) 2011-2024 by John Heidemann <johnh@isi.edu> π
this program is distributed under terms of the GNU general public license, version 2. see the file COPYING with the distribution for details.
okay that's the whole page bestie, ily, go pivot something ππ
π this was Fsdb::F...ilepivot(3) from perl v5.32.1, dated 2024-08-02 β signed, sealed, slayed π
β¨
π¦ where this page comes from
- epel 9: perl-Fsdb 2:3.9-1.el9 Β· GPLv2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms