in reply to New Line at End File
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: New Line at End File
by jmcnamara (Monsignor) on Dec 10, 2002 at 11:20 UTC | |
by tachyon (Chancellor) on Dec 10, 2002 at 13:52 UTC |