Append newline to files
- From: p19010101@xxxxxxxxx
- Date: Mon, 28 Jul 2008 17:41:41 -0700 (PDT)
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?
.
- Follow-Ups:
- Re: Append newline to files
- From: Martijn Lievaart
- Re: Append newline to files
- Prev by Date: Re: Can I upload Perl program in unicode?
- Next by Date: FAQ 4.14 How can I compare two dates and find the difference?
- Previous by thread: Can I upload Perl program in unicode?
- Next by thread: Re: Append newline to files
- Index(es):
Relevant Pages
|