parsowanie pliku – php
October 31st, 2009
Comments off
Mam plik z logami, z których intereuje mnie tylko część danych.
Plik z którego chcę wyciągnąć informacje “init.log” wygląda tak:
Oct 20 14:00:00 ws1-03463fc1e1c newsyslog[93689]: logfile turned over due to size>100K Oct 20 14:34:50 ws1-03463fc1e1c installdb[11924]: started (uid 96) Oct 20 14:34:50 ws1-03463fc1e1c installdb[11924]: Opened receipt database on '/' with schema 17. Oct 20 14:34:57 ws1-03463fc1e1c installdb[11924]: done. (0.006u + 0.005s) Oct 20 15:34:50 ws1-03463fc1e1c installdb[43080]: started (uid 96) Oct 20 15:34:50 ws1-03463fc1e1c installdb[43080]: Opened receipt database on '/' with schema 17. Oct 20 15:34:56 ws1-03463fc1e1c installdb[43080]: done. (0.006u + 0.004s) Oct 20 19:58:27 ws1-03463fc1e1c installdb[28727]: started (uid 96) Oct 20 19:58:27 ws1-03463fc1e1c installdb[28727]: Opened receipt database on '/' with schema 17. Oct 20 19:58:33 ws1-03463fc1e1c installdb[28727]: done. (0.006u + 0.006s) Oct 20 20:58:27 ws1-03463fc1e1c installdb[58388]: started (uid 96) Oct 20 20:58:27 ws1-03463fc1e1c installdb[58388]: Opened receipt database on '/' with schema 17. Oct 20 20:58:33 ws1-03463fc1e1c installdb[58388]: done. (0.007u + 0.006s) Oct 21 08:28:28 ws1-03463fc1e1c installdb[28806]: started (uid 96) Oct 21 08:28:28 ws1-03463fc1e1c installdb[28806]: Opened receipt database on '/' with schema 17. Oct 21 08:28:34 ws1-03463fc1e1c installdb[28806]: done. (0.007u + 0.007s) Oct 21 09:28:27 ws1-03463fc1e1c installdb[59279]: started (uid 96) Oct 21 09:28:27 ws1-03463fc1e1c installdb[59279]: Opened receipt database on '/' with schema 17. Oct 21 09:28:33 ws1-03463fc1e1c installdb[59279]: done. (0.006u + 0.004s) Oct 21 10:28:28 ws1-03463fc1e1c installdb[89960]: started (uid 96) Oct 21 10:28:28 ws1-03463fc1e1c installdb[89960]: Opened receipt database on '/' with schema 17. Oct 21 10:28:34 ws1-03463fc1e1c installdb[89960]: done. (0.006u + 0.006s) Oct 21 11:28:28 ws1-03463fc1e1c installdb[20809]: started (uid 96) Oct 21 11:28:28 ws1-03463fc1e1c installdb[20809]: Opened receipt database on '/' with schema 17. Oct 21 11:28:34 ws1-03463fc1e1c installdb[20809]: done. (0.006u + 0.005s) Oct 21 12:28:29 ws1-03463fc1e1c installdb[51229]: started (uid 96) Oct 21 12:28:29 ws1-03463fc1e1c installdb[51229]: Opened receipt database on '/' with schema 17. Oct 21 12:28:35 ws1-03463fc1e1c installdb[51229]: done. (0.006u + 0.006s) Oct 21 13:28:28 ws1-03463fc1e1c installdb[82151]: started (uid 96) Oct 21 13:28:28 ws1-03463fc1e1c installdb[82151]: Opened receipt database on '/' with schema 17. Oct 21 13:28:34 ws1-03463fc1e1c installdb[82151]: done. (0.006u + 0.004s) Oct 21 14:28:29 ws1-03463fc1e1c installdb[12290]: started (uid 96) Oct 21 14:28:29 ws1-03463fc1e1c installdb[12290]: Opened receipt database on '/' with schema 17. Oct 21 14:28:35 ws1-03463fc1e1c installdb[12290]: done. (0.006u + 0.005s) Oct 21 15:28:30 ws1-03463fc1e1c installdb[43523]: started (uid 96) Oct 21 15:28:30 ws1-03463fc1e1c installdb[43523]: Opened receipt database on '/' with schema 17. Oct 21 15:28:36 ws1-03463fc1e1c installdb[43523]: done. (0.006u + 0.004s) Oct 22 08:33:50 ws1-03463fc1e1c installdb[30573]: started (uid 96) Oct 22 08:33:50 ws1-03463fc1e1c installdb[30573]: Opened receipt database on '/' with schema 17. Oct 22 08:33:56 ws1-03463fc1e1c installdb[30573]: done. (0.006u + 0.006s)
a chcę wyciągnąć z niego linia po linii pełną datę z gofdziną oraz id akcji, np:
Oct 22 08:33:56 installdb[30573]
cała reszta mnie nie interesuje.
Tutaj każda linijka tekstu zaczyna się od daty potem trzeba przeskoczyć kilka znaków i wybrać id_akcji,
można więc wybrać znaki od 1-16, oraz od 32-48, co powinno załatwić sprawę.
tak to może wyglądać:
//wczytaj plik do tablicy
$content = file('init.log');
//wybierz potrzebne znaki z każdej linijki i przeiteruj po nich
foreach($content as $data)
{
echo substr($data, 0, 16);
echo substr($data, 32, 16);
}
wynik:
Oct 20 14:00:00 newsyslog[93689] Oct 20 14:34:50 installdb[11924] Oct 20 14:34:50 installdb[11924] Oct 20 14:34:57 installdb[11924] Oct 20 15:34:50 installdb[43080] Oct 20 15:34:50 installdb[43080] Oct 20 15:34:56 installdb[43080] Oct 20 19:58:27 installdb[28727] Oct 20 19:58:27 installdb[28727] Oct 20 19:58:33 installdb[28727] Oct 20 20:58:27 installdb[58388] Oct 20 20:58:27 installdb[58388] Oct 20 20:58:33 installdb[58388] Oct 21 08:28:28 installdb[28806] Oct 21 08:28:28 installdb[28806] Oct 21 08:28:34 installdb[28806] Oct 21 09:28:27 installdb[59279] Oct 21 09:28:27 installdb[59279] Oct 21 09:28:33 installdb[59279] Oct 21 10:28:28 installdb[89960] Oct 21 10:28:28 installdb[89960] Oct 21 10:28:34 installdb[89960] Oct 21 11:28:28 installdb[20809] Oct 21 11:28:28 installdb[20809] Oct 21 11:28:34 installdb[20809] Oct 21 12:28:29 installdb[51229] Oct 21 12:28:29 installdb[51229] Oct 21 12:28:35 installdb[51229] Oct 21 13:28:28 installdb[82151] Oct 21 13:28:28 installdb[82151] Oct 21 13:28:34 installdb[82151] Oct 21 14:28:29 installdb[12290] Oct 21 14:28:29 installdb[12290] Oct 21 14:28:35 installdb[12290] Oct 21 15:28:30 installdb[43523] Oct 21 15:28:30 installdb[43523] Oct 21 15:28:36 installdb[43523] Oct 22 08:33:50 installdb[30573] Oct 22 08:33:50 installdb[30573] Oct 22 08:33:56 installdb[30573]