sheela b <[email protected]> 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: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/