[Wolves] Echoing first and last line of large file in PHP

Stuart Langridge stuart.langridge at gmail.com
Fri Mar 25 19:18:06 GMT 2005


On Fri, 25 Mar 2005 19:10:19 +0000 (GMT), Adam Sweet <drinky76 at yahoo.com> wrote:
> Hi all, not a Linux question as such but...
> 
> I need to strip out the first and last line of a 500MB
> Apache log file for an assignment, so I can then
> explode it and echo the first date entry and last date
> entry.
> 
> Being as large as it is, I can't use an array as it
> will eat all of the server memory and it takes >30
> seconds to parse the file (this is on a live server).
> 
> Is there a quick way of doing this like the Unix head
> and tail commands?

Why not just shell out to head and tail? This is what they're for, no?

Alternatively, fseek to the end of the file and then do

ch = readOneChar()
while ch <> "\n":
  currentFilePosition -= 1
  fseek(currentFilePosition)
  ch = readOneChar()

or similar.

Aq.



More information about the Wolves mailing list