By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

log file parsing

P: n/a
I have a log file like this:

2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] server:
shutting down server
2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] client:
shutting down client

I need to parse this using php. I have split them by spaces and
everything is put into an array. The problem comes at the end of the
line. Eg. In line 1, server: shutting down server will be parsed as 4
different array values. This will make things ugly when the warning
message is long. Is there any way where I could just display the rest
of the message from a certain point in the line.

regards,
Anush
Aug 26 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Aug 26, 1:18*pm, anush <anushshe...@gmail.comwrote:
2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] client:
shutting down client

I need to parse this using php. I have split them by spaces and
everything is put into an array. The problem comes at the end of the
line. Eg. In line 1, server: shutting down server will be parsed as 4
different array values. This will make things ugly when the warning
message is long. Is there any way where I could just display the rest
of the message from a certain point in the line.
You can use the limit parameter to explode() to limit the number of
elements in the array. Or you can use preg_match() to do some more
advanced splitting and parsing.
Aug 26 '08 #2

P: n/a
On 26 Aug, 12:48, Sjoerd <sjoer...@gmail.comwrote:
On Aug 26, 1:18 pm, anush <anushshe...@gmail.comwrote:
2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] client:
shutting down client
I need to parse this using php. I have split them by spaces and
everything is put into an array. The problem comes at the end of the
line. Eg. In line 1, server: shutting down server will be parsed as 4
different array values. This will make things ugly when the warning
message is long. Is there any way where I could just display the rest
of the message from a certain point in the line.

You can use the limit parameter to explode() to limit the number of
elements in the array. Or you can use preg_match() to do some more
advanced splitting and parsing.
It rather depends where the log is being generated. Looking at the
sample data provided I would suggest exploding around the ':' or using
str_tok on spaces then rebuilding the chunks of data.

Regexes can be a bit expensive - which may be a problem parsing
thousands of lines of log files.

C.
Aug 26 '08 #3

P: n/a
anush wrote:
I have a log file like this:

2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] server:
shutting down server
2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] client:
shutting down client

I need to parse this using php. I have split them by spaces and
everything is put into an array. The problem comes at the end of the
line. Eg. In line 1, server: shutting down server will be parsed as 4
different array values. This will make things ugly when the warning
message is long. Is there any way where I could just display the rest
of the message from a certain point in the line.

regards,
Anush
Should be pretty easy using explode, as long as the format is always
the same as your sample data, otherwise, the limit would need to be
adjusted, or a more dynamic means of parsing would be needed.

<?php
// ...

$data = explode(' ', $line, 5);

// ...
?>

--
Curtis
Aug 26 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.