sheela b <sheela.b.2...@gmail.com> asked:
> How to delete last 10 lines of a file using Perl one liner?
> 
> I used the following one liner to delete first 10 lines of a file,
> 
> perl -i.bak -ne 'print unless 1..10' test.txt

OTTOH:

        perl -i.bak -ne 'BEGIN { $b[9]="" } print shift @b; push @b, $_' 
test.txt

@b is the "bucket chain" of 10 initially empty strings. Inside the implied 
while() loop, new text is added at the end of the chain while the head element 
is pruned and then printed. The loop will terminate on eof(), leaving the last 
10 lines of text in the array.

If you're dealing with small files, another option would be to slurp in the 
file to an array, delete the last 10 items of the array and write the file back 
out to disk.

HTH,
Thomas

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to