It is my experience that most text-processing tools don't preserve newline style (file-copying tools would be a different story). I don't expect it from other tools and I generally don't write tools that do such. If I'm reading newline-terminated lines, then I'm likely processing text, not copying data.

I expect tools to write new text files using the default newline style of the tool's platform. That is certainly what will happen with any tool written in C under Windows (unless the developer goes out of their way to change default behavior).

A simple "perl -pe1" using native Windows perl will include "\r"s in the output even if the input lines are terminated with just "\n"s (note that I didn't even have to give perl the "-l" option).

If you want to preserve newline style, then you want to at least binmode your input and output. Then you can decide whether you want to try to preserve inconsistent or non-standard newline style. I'm not a fan of either so I'd probably just support two output choices, either always "\n" or always "\r\n".

binmode STDIN; binmode STDOUT; my $newline; while( <STDIN> ) { $newline ||= /\r/ ? "\r\n" : "\n"; s/\s+$//; ... print ..., $newline; }

But at this point I don't see ever wanting to do that. :)

- tye        


In reply to Re^3: \r\n at end of line (preserve \r?) by tye
in thread \r\n at end of line by John M. Dlugosz

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.