select * from bodies; #### perl thetable.txt > formatedtable.txt #### #!/usr/bin/perl open BODIES, "$ARGV[0]"; @lines = ; 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);