abrucjusz Posted June 6, 2010 Share Posted June 6, 2010 Hi ! I have hundreds of log files that contain rows of information like these: lt Vermikompostas 7 360 lt Verona 2 93223 lt Veronika 4 20324 lt Veronika_Alseikien 1 7751 lt Verpet 1 7539 lt Verpimo_ratelis 1 7147 lt Vidin_ausis 2 7618 lt Vidin_terp 1 6559 lt Vidinkstas 1 5668 lt Vidmantas_Beokas 1 6944 Other log files include rows with completely new information, like: lt Voronin 3 3349 or new values for existing records, for example: lt Verona 1 3220 lt Veronika 6 93784 I need to sum up all the records and values for them, for instance for 'Veronika' I would have something like: lt Veronika 10 because 4+6 is 10 (the last value for each row in the log records is not required in the summary). It will take me ages to do that manually, so please can anyone help me on the idea about the algorithm or at least point me to the right functions to use? Big Thanks you! Link to comment https://forums.phpfreaks.com/topic/204012-counting-information-from-strings/ Share on other sites More sharing options...
kenrbnsn Posted June 6, 2010 Share Posted June 6, 2010 If you don't care about the second number on each line, here's how I would do it. <?php $final = array(); $files = glob('directoryoflogfiles/*.log'); foreach ($files as $file) { $content = file($file); foreach ($content as $line) { list($lt,$what,$num,$dmy) = explode(' ',trim($line)); if (array_key_exists($what,$final)) { $final[$what] += intval($num); } else { $final[$what] = intval($num); } } } $fp = fopen('newlogfile.log','w'); foreach ($final as $what => $num) { fwrite($fp,"lt $what $num\n"); } fclose($fp); ?> Note: I not checked to syntax errors & not run. Ken Link to comment https://forums.phpfreaks.com/topic/204012-counting-information-from-strings/#findComment-1068539 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.