DBFILEPIVOT(1)
π DBFILEPIVOT(1) π
β¨ NAME β¨
dbfilepivot β pivot a table, converting multiple rows into a single wide row. This is the DBFILEPIVOT(1) page from the User Contributed Perl Documentation crew π«Ά.
π SYNOPSIS π
dbfilepivot [-e empty] -k KeyField -p PivotField [-v ValueField]
πΈ DESCRIPTION πΈ
Okay so dbfilepivot pivots a table: it takes multiple rows that all have the same key and turns them into one single wide row. Slay.
In a normalized database, one might have data with a schema like (id, attribute, value), but sometimes it's more convenient to see the data with a schema like (id, attribute1, attribute2). For example, gnuplot's stacked histograms need denormalized data. dbfilepivot converts the normalized format to 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 clearer, bestie. 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 indicates 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 indicates what goes in the output.
The pivot creates new columns named key_tag1, key_tag2, etc. β one for each tag, where the tag is the contents of the pivot field in the input. It then populates those new columns with the contents of the value field in the input.
If no value column is specified, then values are either empty or 1.
dbfilepivot assumes all lines with the same key are adjacent in the input source, like dbmapreduce(1) with the -S option. To enforce this invariant, by default it requires input be sorted by key.
There is no requirement that the pivot field be sorted (provided the key field is already sorted).
By default, dbfilepivot makes two passes over its data, so it requires temporary disk space equal to the input size. With the --possible-pivots option, the user can specify pivots and skip the second pass and avoid temporary data storage.
Memory usage is proportional to the number of unique pivot values.
The inverse of this commend β yep, that's the original spelling, iconic π β is dbcolsplittorows.
π OPTIONS π
-kor--keyKeyField β specify which column is the key for grouping. Required (no default).-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).-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, bestie:
-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)autorunoption controls that behavior within Perl.--helpβ show help.--manβ show full manual.
Reminder bestie: -S provided twice removes the validation of the βdata is already grouped by keyβ assertion. The safety check is gone, so be cute but be careful π.
π¦ 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
π₯ SEE ALSO π₯
π 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.
Signing off with perl v5.32.1, dated 2024-08-02 β DBFILEPIVOT(1) out π
β¨
π¦ 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