aspose file tools*
The moose likes Beginning Java and the fly likes string search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string search" Watch "string search" New topic
Author

string search

John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
Good evening,

I am looking for some help in understanding my mistake(s).
I am trying to parse a log file to see how many error messages in total I receive per day, that appears to function; however I am also looking to see how many error messages per hour I am getting and that's not wokring the way I would like it to be.



my ouptut is as follows:
please wait while it is performing the calculations
Number of error messages on June 2nd: 1729 errors
Number of messages per hour for 03 am:1 errors
Number of messages per hour for 04 am:1 errors
Number of messages per hour for 05 am:9 errors

for the 03 am I'm suppose to get 8 errors instead of 1 and for 04 am I'm suppose to get 9errors instead of 1 being displayed.
05am having 9 errors is correct but what seems to occur is that when I had a "result = scan.findWithinHorizon("\\s05:\\d\\d:\\d\\d\\s", 0);
" the previous one stops fucntioning correctly.

Could someone please help me figure out where I am going wrong.

THank a lot

John,

PS: the log file used to get the data from is attached.
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
Anybody have any ideas?

Thanks

John
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Could you post a sample of what your log file looks like?

Mike

SCJP 6 || SCWCD 5
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
Thank you Michael,

I tried to attach a sample code as txt, zip, or log and the system always tells me "not allowed as attachement in the message.


the sample txt file looks like this:


Jun 2 03:26:53 macbookscuba Dock[215]: Corrupt JPEG data: premature end of data segment
Jun 2 03:26:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]:
Tue Jun 2 03:26:53 macbookscuba.local Dock[215] <Error>: Corrupt JPEG data: premature end of data segment
Jun 2 03:26:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]:
Jun 2 03:53:53 macbookscuba Dock[215]: Corrupt JPEG data: premature end of data segment
Jun 2 03:53:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]: Tue Jun 2 03:53:53 macbookscuba.local Dock[215] <Error>: Corrupt JPEG data: premature end of data segment
Jun 2 03:53:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]:
Jun 2 04:08:49 macbookscuba Translator[838]: Invoked to sync conduit ExchangeConduit for entityNames: anything
Jun 2 04:20:53 macbookscuba Dock[215]: Corrupt JPEG data: premature end of data segment
Jun 2 04:20:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]: Tue Jun 2 04:20:53 macbookscuba.local Dock[215] <Error>: Corrupt JPEG data: premature end of data segment
Jun 2 04:20:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]:

John
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd consider going about this a different way: the timestamp information is readily available. Why not just grab it and store lists of messages (or a count, or whatever) based on actual dates/times?
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
Hi David,

I did this how I could, I'm very new, and very open on how I do things.
Would you mind presenting me with an example of how your suggestion would work?

Thanks
John
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't have any specifics, but one way to do it would be to have a map of maps of lists: days => hours => log messages. If you don't need the actual log messages then you could just store a count there or something.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

I think I see what's wrong. Whenever you call the Scanner.findWithinHorizon() method, if it finds a match, the Scanner moves to the end of that match. So when you do this:

it finds the first 3 o'clock error message correctly...BUT when it searches for a 4 o'clock message, it skips all the rest of your 3 o'clock messages. This is why it's only returning a count of 1 for 3 o'clock.

Using a Scanner probably isn't the best way to do this...I would use a BufferedReader to read your log line by line.
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
Thank you Michael,

I will try that right now and post back my findings

John
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
I went and tried to scwitch to use the buffereReader but I have to say that it doesn't look like I know how to do it ocrrectly because I am now having all kinds of error message.
How would I go about performing this switch over please?

John
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What error messages are you getting?
John Philippe
Greenhorn

Joined: Apr 06, 2009
Posts: 18
the errors are everywhere (in eclipse the squiggly red) because I don't know how to switch over to the buffereader, and also I lose the ability to use the "scan.findWithinHorizon"
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If the issue is reading in a file line-by-line there are a million examples on the web (I searched for "java +read file lines" and got a good number of results), or using something from Commons-IO makes it trivial.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
John Philippe wrote:Jun 2 03:53:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]: Tue Jun 2 03:53:53 macbookscuba.local Dock[215] <Error>: Corrupt JPEG data: premature end of data segment
Jun 2 03:53:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]:
Jun 2 04:20:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]: Tue Jun 2 04:20:53 macbookscuba.local Dock[215] <Error>: Corrupt JPEG data: premature end of data segment
Jun 2 04:20:53 macbookscuba [0x0-0xa00a].com.apple.dock[215]:


Watch these lines too, I haven't used the Scanner class before but maybe you count twice, since the line contains "Jun 2" two times!

If you want to use a BufferedReader, the general pattern is something like:
1. Create BufferedReader:


2. Iterate through the lines:


You can examine the line using regular expressions as you did in your first example, or also by using the standard string functions (like, line.startsWith("xyz") or line.indexOf("xyz")).

Don't forget to close the Reader at the end (input.close())!
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Don't want to confuse you more, but you also might want to use the Pattern and Matcher classes from the java.util.regex package in order to use regular expressions:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: string search