Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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);
|
---|