perl: print last 10 lines
Posted on 2006-06-08
I am searching through a file line by line:
foreach $line (<FILE>)
if($line eq "something special")
here I want to print out the last 10 lines before $line
I'm new to perl, so I wanted to check with the experts. My guess on the best way to implement this is to use a queue of some kind. Each time a line is read, add it to the queue. When the queue is larger than 10 items, dequeue a line when a new one is added.
Is that the best way to accomplish this? The file I will be reading has tens of thousands of lines, so I want to do this in the most efficient way possible.