[thelist] perl: show me the last n lines from this file
Sam-I-Am
dsentelskids at sam-i-am.com
Fri May 11 15:04:05 CDT 2001
I'm trying to build a page (cgi) that displays the last say 20 access
from the web access-log. (or 5k, or whatever)
I can read in the log file, reverse and print off the top 20 lines --
but the log file is large (today on my local machine it's 10MB, and
could obviously be much bigger another day) and I really don't want all
of it in memory.
Having read my tail manpages, I looked into the File::Tail module but
came out empty handed. (over my head, no clue what to do with it)
I also found this code snippet:
open (SYSLOG, "tac E:/my/log/file.log|") or die "Can't open syslog:
$!,";
while (<SYSLOG>) {
#
# Do something with each line, starting from the tail end...
#
}
I'm on win32, with the cygwin tools installed. But tac is just returning
tac: E:/my/log/file.log
not the contents of the file.
I also looked into perl's read(), but can't figure out (bear with me,
still the newbie here) how/if it can help, as it lets me specify and
offset, but in the variable I'm reading into, not the file I'm reading
from.
I guess I could use while to skip over the first n lines, before I
started populating an array, but that seems very wasteful and silly.
(and to finally stump me I don't have my copy of the cookbook here at
work with me)
I've tried, honest. Anyone got any pointers?
thanks
Sam
More information about the thelist
mailing list