<?xml version="1.0" encoding="windows-1252"?>
<node id="833682" title="Re: Reversing a mysql table" created="2010-04-08 21:48:29" updated="2010-04-08 21:48:29">
<type id="11">
note</type>
<author id="44715">
graff</author>
<data>
<field name="doctext">
Here's a script that I use when I want columns turned into rows and vice-versa (assuming tab delimiters for both input and output).  The code itself is 4 lines (including the shebang line, which covers most of the work), and the rest is docs.
&lt;P&gt;
This is especially handy when I just want to see a row or two from a table with lots of columns, and the first line of data fed to the script is the column labels.
&lt;c&gt;
#!/usr/bin/perl -l -n
@F = split/\t/;
$a[$_][$.-1] = $F[$_] for (0..$#F);
END{ print join "\t", @{$a[$_]} for (0..$#a) }

=head1 NAME

transpose-tsv  -- invert a tab-delimited table

=head1 DESCRIPTION

This stdin-stdout filter assumes that the input is two or more lines
of plain text consisting of tab-separated-values, and that all lines
have a consistent number of fields.  (It doesn't do sanity checks.)

The output will be transposed (or inverted), so that what had been
columns in the input become rows, and vice-versa.  Input like this:

  heading1  heading2  heading3
  value11   value12   value13
  value21   value22   value23
  value31   value32   value33

will be output like this:

  heading1  value11   value21   value31
  heading2  value12   value22   value32
  heading3  value13   value23   value33

=head1 AUTHOR

David Graff

=cut
&lt;/c&gt;
&lt;P&gt;
Update: forgot to mention (in case it's not obvious): given this little script, the only other thing you need is something that will dump your mysql table content to stdout as one tab-delimited row per line, so you can pipe its output to this script.  You probably want that sort of tsv table-dump tool anyway, if you don't have one already.</field>
<field name="root_node">
833265</field>
<field name="parent_node">
833265</field>
</data>
</node>
