Re: Append newline to files



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
.



Relevant Pages

  • Re: Absolute Novice (Mac Tiger) Needs Help
    ... MacOS X is so newlines are linefeeds ... Windows didn't do the same because line the termination is two characters ... the meaning of \n and \r remain the ... to believe it until I can test it on a dodgy newline system such as ...
    (comp.lang.perl.misc)
  • Re: Forcing list context on <$fh>
    ... Please read this extract from "perldoc perlport": ... "Perl uses \n to represent the "logical" newline, ... It's been a _long time_ since I've used Windows. ... Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers ...
    (comp.lang.perl.misc)
  • Re: Environment.Newline
    ... non-Windows implementations of the C# language, compiler, and runtime environment. ... it compiles and runs anywhere Unix or Windows does. ... True, there are only two types of newline currently if you only count the latest PC operating systems, and hopefully that narrows to one in the future, but should it be the responsibility of the programmer to determine what operating system he is on and write logic that outputs the currect system newline? ... Ok, I can see in the docs that it represents "newline" for the current platform - I assume that it is a runtime property, and not compile time? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Forcing list context on <$fh>
    ... Please read this extract from "perldoc perlport": ... "Perl uses \n to represent the "logical" newline, ... Doesn't it say that STDIO changes the meaning of \n on Windows? ... It's been a _long time_ since I've used Windows. ...
    (comp.lang.perl.misc)
  • Re: IO#putc writing 2 bytes?
    ... Problem is for some reason putc is writing 2 bytes ... Looks like you are on Windows. ... Try opening @SaveFile in binary mode ... converted to the OS's newline, which for windows is 0A 0D. ...
    (comp.lang.ruby)