tags:

views:

20

answers:

1

I am working on a PHP script that needs to read log files in reverse line order.

I currently do the following:

<?php
shell_exec("tac logfile.log > tmpfile.log");
$rFile = fopen("tmpfile.log", "r");
while (!feof($rFile))
{
    //logic 
}
unlink("tmpfile.log");
?>

This works nicely as it switches the order of the lines in the file and I read from the temp file.

However, the log files are going to get massive and I need to keep a lengthy history, so I need to gzip the files up. I found out about 'zcat', and I was hoping that there would be 'ztac' which could plug straight into my code above... but I haven't managed to find it.

Any ideas what the easiest/best way to do this is without needing lots of temp files and a big mess of server commands?

+2  A: 

I'm thinking

zcat logfile.log.gz | tac > tmpfile.log

Unzip and reverse lines as two steps.

David Zaslavsky
Awesome, that'll do the job :)Thanks heaps!
Valorin
That's the UNIX philosophy at work - each tool does one thing, and when you need to combine effects, you chain them together like that. Glad that it works!
David Zaslavsky