Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > parse a log file question

Reply
Thread Tools

parse a log file question

 
 
robertchen117@gmail.com
Guest
Posts: n/a
 
      03-01-2007
hi all,

the log file format is a long list of the item like:
Fri Oct 6 00:00:23 2006: TR_execute_task( Lib/Task: library_name1/
task_name2 run by www.hp.com on 7 targets, out = 257

I want to count the number of targets based on library_name1/
task_name2, but there are many library names and task names, so the
array size not know. How could I do this?

My perl codes is like this:

.....
open (TSK, "more /tmp/task28.log | grep '$now' | grep 'out =' |") or
die "cannot fork: $!";

while (<TSK>) { # count them up.
($foo,$foo,$foo,$time,$foo,$foo,$foo,$taskinfo,$fo o,$foo,$foo,
$foo,$targets,$foo,$foo,$foo, $bytes) = split ' ';

should be like array[$taskinfo] += $targets;

How could I define the array or hash dynamicly?

Please help me, thanks.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      03-01-2007
On Mar 1, 7:50 am, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> hi all,
>
> the log file format is a long list of the item like:
> Fri Oct 6 00:00:23 2006: TR_execute_task( Lib/Task: library_name1/
> task_name2 run bywww.hp.comon 7 targets, out = 257
>
> I want to count the number of targets based on library_name1/
> task_name2, but there are many library names and task names, so the
> array size not know.


Perl couldn't care less what the size of the array is. Arrays grow
and shrink dynamically.

> How could I do this?
>
> My perl codes is like this:
>
> ....
> open (TSK, "more /tmp/task28.log | grep '$now' | grep 'out =' |") or
> die "cannot fork: $!";
>
> while (<TSK>) { # count them up.
> ($foo,$foo,$foo,$time,$foo,$foo,$foo,$taskinfo,$fo o,$foo,$foo,
> $foo,$targets,$foo,$foo,$foo, $bytes) = split ' ';


GAH!

my ($time, $taskinfo, $targets, $bytes) = (split ' ')[3,7,12,16];

> should be like array[$taskinfo] += $targets;
>
> How could I define the array or hash dynamicly?


Other than the missing dollar sign, what exactly is wrong with that
statement? Did you try it out, or are you simply assuming it doesn't
work?

Please post a short-but-complete script that demonstrates the error
you're experiencing.

Paul Lalli

 
Reply With Quote
 
 
 
 
robertchen117@gmail.com
Guest
Posts: n/a
 
      03-01-2007
Sorry my question should be like this:
Fri Oct 6 00:00:23 2006: TR_execute_task( Lib/Task: library_name1/
task_name2 run bywww.hp.comon7 targets, out = 257

I know I can use $task_hash{$taskinfo} += targets; -->count by lib/
task name.
but problems is I also need to count by time:
output should like this:

9:00am - 9:59am
8,010 task2 lib3 77 targets 200,345 output
3,221 task12 lib3 12 targets 1,530,234 output
....

Do we have hash over hash? What should we implement this?


On 3月1日, 下午9时05分, "Paul Lalli" <(E-Mail Removed)> wrote:
> On Mar 1, 7:50 am, "(E-Mail Removed)" <(E-Mail Removed)>
> wrote:
>
> > hi all,

>
> > the log file format is a long list of the item like:
> > Fri Oct 6 00:00:23 2006: TR_execute_task( Lib/Task: library_name1/
> > task_name2 run bywww.hp.comon7 targets, out = 257

>
> > I want to count the number of targets based on library_name1/
> > task_name2, but there are many library names and task names, so the
> > array size not know.

>
> Perl couldn't care less what the size of the array is. Arrays grow
> and shrink dynamically.
>
> > How could I do this?

>
> > My perl codes is like this:

>
> > ....
> > open (TSK, "more /tmp/task28.log | grep '$now' | grep 'out =' |") or
> > die "cannot fork: $!";

>
> > while (<TSK>) { # count them up.
> > ($foo,$foo,$foo,$time,$foo,$foo,$foo,$taskinfo,$fo o,$foo,$foo,
> > $foo,$targets,$foo,$foo,$foo, $bytes) = split ' ';

>
> GAH!
>
> my ($time, $taskinfo, $targets, $bytes) = (split ' ')[3,7,12,16];
>
> > should be like array[$taskinfo] += $targets;

>
> > How could I define the array or hash dynamicly?

>
> Other than the missing dollar sign, what exactly is wrong with that
> statement? Did you try it out, or are you simply assuming it doesn't
> work?
>
> Please post a short-but-complete script that demonstrates the error
> you're experiencing.
>
> Paul Lalli



 
Reply With Quote
 
J黵gen Exner
Guest
Posts: n/a
 
      03-01-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> the log file format is a long list of the item like:
> Fri Oct 6 00:00:23 2006: TR_execute_task( Lib/Task: library_name1/
> task_name2 run by www.hp.com on 7 targets, out = 257
>
> I want to count the number of targets based on library_name1/
> task_name2, but there are many library names and task names, so the
> array size not know.


So what? Arrays in Perl grow dynamically.

> My perl codes is like this:
> ....
> open (TSK, "more /tmp/task28.log | grep '$now' | grep 'out =' |") or
> die "cannot fork: $!";


Ahhhhh! Is there a specific reason why you are shelling out 3 external
processes for a task that could be done by two lines of Perl internally?
Besides, that's a totally useless use of more(1).
And where is $now coming from?

Suggestion:
open (TSK, "/tmp/task28.log") or die "cannot fork: $!";

> while (<TSK>) { # count them up.


Suggestion continued:
if (/$now/ and /out =/) {

> ($foo,$foo,$foo,$time,$foo,$foo,$foo,$taskinfo,$fo o,$foo,$foo,
> $foo,$targets,$foo,$foo,$foo, $bytes) = split ' ';


If you need a placeholder for a dummy value it is customary to use undef.
Beside, you can index the result from split like a array. I suggest you read
up on "array slice", too. Oh, and while you are at it check out the
documentation for split(), too. It defaults to split on whitespace if the
pattern is missing.
So you want something like
($taskinfo) = (split)[7];

> should be like array[$taskinfo] += $targets;


$taskinfo is a string, isn't it? The numerical value of a string is (unless
it starts with a number) always 0. In other words you are overwriting
array[0] over and over again.
Did you mean to use a hash instead?

> How could I define the array or hash dynamicly?


You don't. Perl arrays and hashes are always dynamic.

jue


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      03-01-2007
On Mar 1, 8:15 am, "(E-Mail Removed)"

> Do we have hash over hash? What should we implement this?


I can't understand the vast majority of your post - that's why I told
you to post a short-but-complete script, which you ignored - so I'll
just answer this question: Yes, hashes of hashes are valid. Please
read:
perldoc perlreftut
perldoc perllol
perldoc perldsc

Paul Lalli


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse a log file kaklis@gmail.com Python 3 01-18-2010 10:46 PM
How to parse the log file to get useful information using perl or shell? robertchen117@gmail.com Perl Misc 1 03-26-2007 09:54 AM
How to parse this postgresql log file? Joost Kraaijeveld Java 3 12-09-2006 07:10 PM
Urgent Pls: Facing problem in reading Log information from Log file, created by IIS Amratash ASP .Net 0 04-13-2004 09:08 AM
Help using regular expressions to parse complex log file. Max Adams Perl Misc 4 08-29-2003 12:22 PM



Advertisments