Re: Parsing a log file
From: Karl von Laudermann (karl_at_ueidaq.com)
Date: 11/21/03
- Next message: Sudsy: "Re: Tomcat 4.1: how to limit url-pattern to a single directory"
- Previous message: Bryan E. Boone: "Re: Here's a class for streaming to a TextArea. Comments?"
- In reply to: Prabh: "Parsing a log file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 21 Nov 2003 07:07:00 -0800
Prab_kar@hotmail.com (Prabh) wrote in message news:<e7774537.0311201023.6f34640c@posting.google.com>...
> Hello all,
> I need to parse a log file and generate a formatted output.
> I do have a solution in PERL, but now need to transform it to Java.
> Could anyone please direct how do I go about it.
>
> I have a log file of following format, which contains info. on a
> series of files after a process.
>
> ========================================================
> File1: Info. on File1
> File2: Info. on File2
> File1: Info. on File1
> File3: Info. on File3
> File1: Info. on File1
> and so on...
> ========================================================
>
> I want to display the output as...
>
> ============================
> n1 lines of info on File1
> n2 lines of info on File2
> n3 lines of info on File3
> ============================
<snip>
> 2) Whats the diff. b/w an array, vector, HashMap etc.?
> Is there any thumb rule about when should one use which collection?
An array has a fixed size at the time of creation. A Vector is
dynamic, so you can simply append items to the end of it by using the
add method, and it will grow automatically. A HashMap or Hashtable
allows you to store and retrieve value objects using key objects
rather than simple numeric indices, and the values are unordered.
Offhand, I would implement your program the following way: Use a
Hashtable, where the filenames in the log are used as keys, and an
Integer object containing the line count for a file is used as its
value. As you read each line of the log, parse out the file name and
retrieve the line count from the Hashtable, using the file name as the
key. If a line count doesn't exist for that file name yet, store one
with the value of 1. If it does exist, replace it with an Integer
whose value is 1 higher than the old one. Repeat until you've read the
entire file.
At this point, you can use the keys method of Hashtable to get the
filenames. For each file you can get the line count and print it.
Of course, this all assumes that you don't want to store the lines and
do other useful things with them. If you do, then use a Hashtable to
map each filename to a Vector containing the lines for that file
instead.
Hope this helps.
- Next message: Sudsy: "Re: Tomcat 4.1: how to limit url-pattern to a single directory"
- Previous message: Bryan E. Boone: "Re: Here's a class for streaming to a TextArea. Comments?"
- In reply to: Prabh: "Parsing a log file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|