How about this:
#!/usr/bin/perl -w
use strict;
my ($line, $spacer);
while(defined($_=<>)) {
s/\n//g;
if ($_ =~ /^[0-9]{3,5}/) {
print "$line\n" if defined($line);
$line=$_;
} else {
$spacer = (/^\|/ || $line =~ /\|$/) ? "" : " ";
$line.="$spacer$_";
}
}
print "$line\n"; #get last one...
If a line starts or ends with a pipe ("|"), then it assumes you do not want extra whitespace around it. Otherwise, it should honor any leading and trailing whitespace on each line (so if one line ends with a tab, and the next starts with one, the code will spit out two tabs in a row).