You think your solution works because your script to create the test file and your script to dump the test file suffers from the same bug as your solution. Your demonstration only shows that you can convert CRCRLF to CRLF.

The first f.txt contains foo\r\r\nbar\r\r\nstuff.

>perl -e "print \"foo\r\nbar\r\nstuff\";" > f.txt >debug f.txt -rcx CX 0011 : -d100 l11 0AF7:0100 66 6F 6F 0D 0D 0A 62 61-72 0D 0D 0A 73 74 75 66 foo...bar +...stuf 0AF7:0110 66 f -q

You alter it to foo\r\nbar\r\nstuff.

>perl -i.bak -pe "s/\r(?=\n)//" f.txt >debug f.txt -rcx CX 000F : -d100 lf 0AF7:0100 66 6F 6F 0D 0A 62 61 72-0D 0A 73 74 75 66 66 foo..bar. +.stuff -q

why doesn't it work??

Unless you use binmode on your input file, CRLF gets converted to LF on read.
Unless you use binmode on your output file, LF gets converted to CRLF on write.


In reply to Re^4: Remove CR from file on Windows by ikegami
in thread Remove CR from file on Windows by kp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.