Re: Append newline to files
- From: Martijn Lievaart <m@xxxxxxxxxxxxxxxx>
- Date: Tue, 29 Jul 2008 21:51:40 +0200
On Mon, 28 Jul 2008 17:41:41 -0700, p19010101 wrote:
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?
The latter. And what are you going to with a file without any newlines?
1) Better s/0x0d//g, make all the files the same format.
2) Or investigate if it are only the Windows files missing a trailing
lineseparator, it's a typical Windows problem.
M4
.
- Follow-Ups:
- Re: Append newline to files
- From: Jürgen Exner
- Re: Append newline to files
- References:
- Append newline to files
- From: p19010101
- Append newline to files
- Prev by Date: Re: what is the expression mean?
- Next by Date: Re: Append newline to files
- Previous by thread: Append newline to files
- Next by thread: Re: Append newline to files
- Index(es):
Relevant Pages
|