I have always found a solution that works by searching this site and others, but this problem has been driving me crazy for a few days. I'm coding in Windows (Windows 10) and trying to change a Windows created text file that ends in CRLF to just LF, so that I can upload the file and process it on a Unix-based site. Every thing that I have tried either gives me CRLF back, or in some cases I can get the file to end with just CR, but never just LF.


I know that this script is lengthy, but the oneliners haven't worked, so I was attempting to pull in the existing file, substitute CRLF with LF, push the result onto an array, then re-open the original filename and dump the array. Here's what doesn't work ( I have an environement variable set which we can say rbfile=myfile.txt ):


use strict; use warnings; my $rxbfile = $ENV{rbfile}; open(IN, "$rxbfile") or die $!; binmode IN; my @array = ""; my $newline = ""; while (<IN>) { $newline = $_; $newline =~ s/\r\n/\012/; push @array,$newline; }; close (IN); open(OUT, ">$rxbfile") or die $!; foreach (@array) { print OUT $_; }; close (OUT);

In reply to Changing CRLF in Windows to LF for UNIX by Anonymous Monk

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.