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:select * from bodies;
here it be:perl thetable.txt > formatedtable.txt
Enjoy!#!/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);
Maloi
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Straighten MySQL Table Displays
by kirbyk (Friar) on Jul 18, 2000 at 22:14 UTC |