Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.

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.

#!/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

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.


In reply to Re: Reversing a mysql table by graff
in thread Reversing a mysql table by gogoglou

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (10)
    As of 2014-09-16 15:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (33 votes), past polls