We help IT Professionals succeed at work.

Test if unique ID is in log file

trevor1940
trevor1940 asked
on
113 Views
Last Modified: 2017-03-09
I have a log file something like bellow

How do I test if an ID exist and type = ABC?

log.txt

ID|TYPE|DATE
123|XYZ|2017-03-07
123|ABC|2017-03-08
124|XYZ|2017-03-08

Open in new window


PHP

<?php
error_reporting(E_ALL);
$Data = explode('|', file_get_contents("log.txt"));
trace($Data);
$ID = 123;
if($Data[$ID]){
  echo "found $ID";
  }

function trace($arr){
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}//end trace

Open in new window

Comment
Watch Question

Consultant
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
I hadn't realized the size of the file mattered

The log file currently doesn't exist so can be in what  ever format I want but is likely to grow daily as each time the application is run new lines will be created maybe 10 per day.

Maybe I need further explanation

each time it's run a directory is read with 1 or more files in
within each file a ID and TYPE is read
I need to test it hasn't been processed  before potentially the same ID can be used up to 3 times depending on type  however the vast majority will be once

so I would open the log file once and as I read the directory test if that file ID / TYPE has been processed before if not then process the file
Most Valuable Expert 2011
Author of the Year 2014

Commented:
"Size matters" within some broad constraints.  When we're talking about adding 10 lines a day to a file, it will take years before size matters!  Any of the solutions shown here will probably be OK, but as usual, the devil is in the details.  If the test case we worked with is not truly representative of the live data, then there may be additional considerations and changes needed.

Author

Commented:
Thanx for your help and info