If you don't want to read the whole file into memory you can just truncate it in place....

[tachyon@www cgi-bin]# cat ./test.pl #!/usr/bin/perl my $file = '/tmp/test.txt'; open FILE, ">$file" or die $!; print FILE "foo\nbar\nfoo\nbar\nfoo\nbar\nfoo\nbar\nfoo\nbar\nfoo\nbar +\n\n\r\n\n\r"; close FILE; # grab the last 20 bytes for analysis open FILE, $file or die $!; { local $/ = undef; seek FILE, -20, 2; $EOF = <FILE>; } close FILE; my $file_length = -s $file; (my $newlines) = $EOF =~ m/([\015\012]+)\z/; my $num_newlines = length $newlines; print "File length $file_length bytes, number of newlines $num_newline +s\n"; truncate $file, ($file_length - $num_newlines) or die $!; print "Now file is ", -s $file, "bytes\n\n"; [tachyon@www cgi-bin]# ./test.pl File length 53 bytes, number of newlines 6 Now file is 47 bytes [tachyon@www cgi-bin]#

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: New Line at End File by tachyon
in thread New Line at End File by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.