I recently came across a situation where I had to get all the rows from a table in mysql, using something like
select * from bodies;
The problem was, I was doing this over a network connection and only had the logging ability of my ssh client to capture the output, which mangled the tables horribly by wrapping every line and somehow managed to get some of the line lengths mixed up. I actually retrieved several tables like this, and this was pretty much my only option (at the time) for getting them. So I wrote this little bit of perl code that figures out the number of fields and size of each field based on the top line of the table and reformats it into a nice, easily readable form... It was a quick hack, and isn't necessarily perfect - for instance you MUST put the table in its own text file, and it MUST start at the very first character in the file... It also assumes that MySQL outputs its line seperators in a format like "+---+---+---+" and every other line like: "|---|---|---|". But it works, so I thought I'd share... It works like this:
perl thetable.txt > formatedtable.txt
here it be:
#!/usr/bin/perl open BODIES, "$ARGV[0]"; @lines = <BODIES>; close BODIES; $body = join("", @lines); $fieldsDefStr = substr($body, 0, index($body, "\|") - 1); $fieldsDefStr =~ s/\n//g; $fieldsDefStr =~ s/\r//g; $fieldsDefStr =~ s/[ ]//g; $pos = -1; $counter = 0; while ( ($pos = index($fieldsDefStr, "+", $pos)) > -1 ) { $fieldPos[$counter] = $pos; $counter++; $pos++; } $numFields = @fieldPos; for ( $counter = 1; $counter < $numFields ; $counter++ ) { $fieldLength[$counter-1] = $fieldPos[$counter] - $fieldPos[$counter +-1] - 1; } $pipeRegEx = "\\\|"; $plusRegEx = "\\+"; $packFormat = "A1"; for ( $counter = 1; $counter < $numFields ; $counter++ ) { $pipeRegEx .= "[^\|]*\\\|"; $plusRegEx .= "[^+]*\\+"; $packFormat.= " A$fieldLength[$counter-1] A1"; } $body =~ s/\n//g; $body =~ s/\r//g; $body =~ s~($pipeRegEx)~$1\n~g; $body =~ s~($plusRegEx)~$1\n~g; @lines = split(/\n/, $body); foreach $line (@lines) { $line =~ s/^[ ]*//; if ( index($line, "|") == 0 ) { @tempLine = split(/\|/, $line); splice(@tempLine, 0, 1); undef @finalLine; foreach $field (@tempLine) { push @finalLine, "\|"; push @finalLine, $field; } push @finalLine, "\|"; $line = pack $packFormat, @finalLine; } if ( index($line, "+") == 0 ) { $line =~ s/[ ]//g; } print "$line\n"; } exit(0);
Enjoy!

Maloi


In reply to Straighten MySQL Table Displays by maloi

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.