Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Reversing a mysql table

by graff (Chancellor)
on Apr 09, 2010 at 01:48 UTC ( #833682=note: print w/ replies, xml ) Need Help??


in reply to Reversing a mysql table

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.


Comment on Re: Reversing a mysql table
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://833682]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2015-07-30 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls