0

I have a text file like:

================================================
[Feb 11 2013 13:17:14] - some string here and here
General options - [something]
Line y
================================================
Line 1
Line 2
Line 3
Line 4
Line 5     
Something here. Error message: ReferenceError: applyMetadataTemplate is undefined; line: 625  
Line 7
================================================
[Feb 11 2013 16:07:14] - some string here and here
General options - [something]
Line y
================================================
Line 1
Line 2
Line 3
Line 4
Line 5     
Something here. Error message: ReferenceError: applyMetadataTemplate is undefined; line: 625  
Line 7

Now I want to read this file backwards and for each Error that I find inside a record with a new date, I need to do something. I need help 1) reading the file backwards until I encounter the date and saving that date, 2) Grabbing all the lines until then as a string and finding the word Error inside it. Note: Each record may have different number of lines and may not necessarily have the word error inside it. It's more a "finding and matching the date and then finding error inside that record" type of problem.

1 Answers1

1
$matches = array();    
$file = file("log.txt");
$file = array_reverse($file);
foreach($file as $f){
    if (stripos($f, "[") !== false) { 
        // Check string for date by regex 
        preg_match('/(\D*) (\d{2}) (\d{4})/', $f, $matches);

        // Check that parts of the date were found
        if(count($matches) > 2) {
            echo $f; //print the line
            break;
        }
    }
}

Read a file, and convert it into an array, then reverse the array for backward traversal, then print out the last date.

Husman
  • 6,819
  • 9
  • 29
  • 47
  • I kinda know how to do that (see the tags in the question)! Its finding the error and date that's giving me the head ache! :( – Watchful Protector Feb 12 '13 at 16:06
  • Any better? I've done keyword search's for the word 'error' and the '[' character to locate errors and dates. – Husman Feb 12 '13 at 16:11
  • edited to break out of the loop when it finds the first date (which is the last date since we are reading it backwards) – Husman Feb 12 '13 at 16:40
  • The problem with breaking it at first occurrence is that there is a line just before the date that has a `[]` too. I didn't think of this logic and that's why I did not include it. So, how do I grab the whole line that has the date? – Watchful Protector Feb 12 '13 at 16:48
  • Also, thinking now, I don't think I need the `break` in the code . I would just grab the SECOND line each time for the line that holds the date. But, how do I grab the ENTIRE line? `echo $f[1]` doesn't display the second line!! :/ – Watchful Protector Feb 12 '13 at 17:00
  • can you please help me out here? :| – Watchful Protector Feb 12 '13 at 17:15
  • Try the code above, it starts off finding lines containing '[' but then it only match lines containing dates in the 'MMM DD YYYY' format and print that line out, before break; and exiting the loop. – Husman Feb 12 '13 at 17:19