Chris Wagner wrote:

> 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]+$//;

> Here's how I would repair ur file.
> 
> open(FILE, "$file");
> open(FILE2, "> $file2");
> binmode FILE;
> binmode FILE2;

Don't need the binmode on the output file unless you want to remove \r's.

> while (read FILE, $buffer, 32768) {
>         $buffer =~ s/\cM\cM\cJ/\cM\cJ/g;

I would use something like for this situation:
        $buffer =~ s/\r*\n/\n/gs;
or
        $buffer =~ s/\r*\n\r*/\n/gs; # for more complicated endings
or maybe even better, just remove all the \r's:
        $buffer =~ tr/\r//d;

There is also my earlier split solution for smaller files.

>         print FILE2 $buffer;
> }
> 
> This can actually be combined with ur linecount insertion.

The line count insertion was my request so he could compare
what was a line and what wasn't - not needed.

> 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.

\n is indeed a character.  It's a linefeed/newline character.
It's the underlying OS that adds the CR/\r in front of the
LF/\n when in text mode.

My philosophy would be to never use a \r ever when coding
output (except to overwrite a line).  To cleanup a bad file
like the above, I would just read it in and replace all of
the \r\n \n\r \r\n\r (or anything similar) sequences and
write it back out with a single newline in text mode.  Unless
of course you want UNIX line endings in which case I would
binmode the output file and not have the \r added.


-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to