Append newline to files

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?
 
M

Martijn Lievaart

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
 
J

Jürgen Exner

Martijn Lievaart said:
2) Or investigate if it are only the Windows files missing a trailing
lineseparator, it's a typical Windows problem.

And here you just fell into the trap.

If "\n" is a line _seperator_, then a trailing line separator would
simply indicate another emtpy line at the end of the file:
"foo\nbar\n"
would contains 3 lines, 'foo', 'bar' and an empty line.

If on the other hand "\n" is a line _terminator_ then the above string
would contain 2 lines. And
"foo\nbar\n\buzz"
would still be only two lines with some trailing junk.

jue
 
M

Martijn Lievaart

And here you just fell into the trap.

If "\n" is a line _seperator_, then a trailing line separator would
simply indicate another emtpy line at the end of the file:
"foo\nbar\n"
would contains 3 lines, 'foo', 'bar' and an empty line.

If on the other hand "\n" is a line _terminator_ then the above string
would contain 2 lines. And
"foo\nbar\n\buzz"
would still be only two lines with some trailing junk.

You're right. But on Windows, it's a line separator[1]. On Unix, it's a
line terminator. So there is no correct way.

M4

[1] Although many programs follow Unix conventions, so it's ill advised
to rely on this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top