At 06:31 PM 6/23/05 -0700, $Bill Luebkert wrote:
>000150 3a 32 37 3a 32 32 09 53 79 73 74 65 6d 20 73 6f :27:22.System so
>000160 72 74 20 73 70 65 63 3a 20 0d 0d 0a 61 31 35 62 rt spec: ...a15b
>000170 36 30 30 0d 0d 0a ^^^^^^^^ 600...
>
>Notice the double \r and \n at the 000160 line. They probably don't
>belong there. Also notice that all your line endings have an extra
>\r.
Now I see what ur trying to do and what ur up against. I've run into the
corrupt end of line problem before too. Especially in cross platform
environments. A simple chomp isn't enough to fix it. So I came up with a
more powerful chomp2 function that can fix any line end garbage.
$line =~ s/[\cM\cJ]+$//;
>Program output:
>6: ObjMgrSqlLog Debug 5 2005-06-18 10:27:22 User sort spec:
Locale Code
>7: ObjMgrSqlLog Debug 5 2005-06-18 10:27:22 System sort spec:
>8: a15b600
>
>Line 8 is caused by the \r\r\n at the end of line 7 (see dump).
>There's no easy way to fix the line 7-8 error, but you can fix the
>double \r error by just changing the print above to
> print "$_\n";
>and redirecting to a new file. The new file should have \r\n on each
>line rather than \r\r\n.
Here's how I would repair ur file.
open(FILE, "$file");
open(FILE2, "> $file2");
binmode FILE;
binmode FILE2;
while (read FILE, $buffer, 32768) {
$buffer =~ s/\cM\cM\cJ/\cM\cJ/g;
print FILE2 $buffer;
}
This can actually be combined with ur linecount insertion.
open(FILE, "$file");
open(FILE2, "> $file2");
binmode FILE;
binmode FILE2;
$line = 2;
while (read FILE, $buffer, 32768) {
$buffer = "1: $buffer" and ++$beenthere unless $beenthere;
$buffer =~ s/\cM\cM\cJ/\cM\cJ/g;
while ($buffer =~ s/\cM\cJ(?!\d+: )(?=.)/\cM\cJ$line: /) {
$line++;
}
print FILE2 $buffer;
}
After thinking about that a bit I realized it can be tricked by ur input
data already having a "\cJ\d: " sequence in it. This one is a little more
robust but not as elegant.
open(FILE, "$file");
open(FILE2, "> $file2");
binmode FILE;
binmode FILE2;
$line = 2;
while (read FILE, $buffer, 32768) {
$buffer =~ s/\cM\cM\cJ/\cM\cJ/g;
@buf2 = split /\cM\cJ/, $buffer;
if ($#buf2 >= 1) {
foreach $i (@buf2) {
print FILE2 "${line}: $i\cM\cJ" and $line++ if $beenthere;
print FILE2 "1: $i\cM\cJ" and ++$beenthere unless $beenthere;
}
}
else {
print FILE2 $buf2[0] if $beenthere;
print FILE2 "1: $buf2[0]" and ++$beenthere unless $beenthere;
}
}
Whenever we're in these situations where we're in doubt about what's
actually being read/wrote I use \cM and \cJ and binmode rather than the
print escapes. \n is not a character but the system local newline sequence.
When working on raw data it's best to stick to bytes.
--
REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=--
"...ne cede males"
00000100
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs