P
p19010101
I need to go through few hundred text files and append newline for
files that don't end with newline. So I have the following code to
read the file:
local $/ = undef;
open(handle, $filename) || die($!);
$content = <handle>;
close(handle);
Then use the following to check:
if($content =~ m/.*\n$/)
{
# file end with newline, do nothing
}
else
{
open(handle, ">>", $filename) || die($!);
print handle "\n";
close(handle);
}
The above code is working fine but I noticed some of the files have LF
as newline (probably originated from Unix) while the others are CRLF
(Windows). The perl script will run on a Windows machine with
ActiveState Perl 5.6, and \n is always CRLF when written to file.
Is there a way to tell Perl to append LF for Unix files and CRLF for
Windows files?
Or do I have to specifically look for \x0d\x0a and then append \x0d or
\x0d\x0a accordingly?
files that don't end with newline. So I have the following code to
read the file:
local $/ = undef;
open(handle, $filename) || die($!);
$content = <handle>;
close(handle);
Then use the following to check:
if($content =~ m/.*\n$/)
{
# file end with newline, do nothing
}
else
{
open(handle, ">>", $filename) || die($!);
print handle "\n";
close(handle);
}
The above code is working fine but I noticed some of the files have LF
as newline (probably originated from Unix) while the others are CRLF
(Windows). The perl script will run on a Windows machine with
ActiveState Perl 5.6, and \n is always CRLF when written to file.
Is there a way to tell Perl to append LF for Unix files and CRLF for
Windows files?
Or do I have to specifically look for \x0d\x0a and then append \x0d or
\x0d\x0a accordingly?