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

Replies are listed 'Best First'.
RE: Straighten MySQL Table Displays
by kirbyk (Friar) on Jul 18, 2000 at 22:14 UTC
    If you have to do this again, and for some reason can't use the DBI module to connect to MySQL (which makes this problem easy!), you might try looking at the built-in mysql tool 'mysqldump'. It's a much better design for getting this kind of data dump - you can specify your own field separators, and basically have a lot more options that might make things signifigantly easier to format.

    Of course, if you _can_ run a perl script on a machine that is allowed to talk to the MySQL database (and you can configure MySQL to allow particular remote machines in - it's mildly complex, but the docs on mysql.com do spell it out), then things become nice. Use the DBI module, and you can read the fields directly into an array, which allows you to take full advantage of perl's formatting.

    Not that your script isn't useful - I've written similar, to get to a brio database in the unfortunate past - but with mysql, you hopefully don't have to work that hard.

    -- Kirby

    Tuxtops: Laptops with Linux!