DBFILEPIVOT(1)

πŸ“¦
version 3.9 Β· 3 versions
, in epel 9

πŸ’– 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 πŸ’…

  • -k or --key KeyField β€” specify which column is the key for grouping. Required (no default).
  • -p or --pivot PivotField β€” specify which column is the key to indicate which column in the output is relevant. Required (no default).
  • -v or --value ValueField β€” specify which column is the value in the output. If none is given, 1 is used for the value.
  • --possible-pivots PP β€” specify all possible pivot values as PP, a whitespace-separated list. With this option, data is processed only once (not twice).
  • -C S or --element-separator S β€” specify the separator S used to join the input's key column with its contents. Defaults to a single underscore.
  • -e E or --empty E β€” give value E as the value for empty (null) records.
  • -S or --pre-sorted β€” assume data is already grouped by key. Provided twice, it removes the validation of this assertion. By default, we sort by key.
  • -T TmpDir or --tmpdir TmpDir β€” where to put tmp files. Also uses environment variable TMPDIR, if -T is not specified. Default is /tmp.

This module also supports the standard fsdb options, bestie:

  • -d β€” enable debugging output.
  • -i or --input InputSource β€” read from InputSource, typically a file name, or "-" for standard input, or (if in Perl) a IO::Handle, Fsdb::IO or Fsdb::BoundedQueue objects.
  • -o or --output OutputDestination β€” write to OutputDestination, typically a file name, or "-" for standard output, or (if in Perl) a IO::Handle, Fsdb::IO or Fsdb::BoundedQueue objects.
  • --autorun or --noautorun β€” by default, programs process automatically, but Fsdb::Filter objects in Perl do not run until you invoke the run() method. The --(no)autorun option 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 πŸ”₯

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