parse a log file question

Discussion in 'Perl Misc' started by robertchen117@gmail.com, Mar 1, 2007.

  1. Guest

    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,$foo,$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.
     
    , Mar 1, 2007
    #1
    1. Advertising

  2. Paul Lalli Guest

    On Mar 1, 7:50 am, "" <>
    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,$foo,$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
     
    Paul Lalli, Mar 1, 2007
    #2
    1. Advertising

  3. Guest

    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" <> wrote:
    > On Mar 1, 7:50 am, "" <>
    > 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,$foo,$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
     
    , Mar 1, 2007
    #3
  4. 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,$foo,$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
     
    Jürgen Exner, Mar 1, 2007
    #4
  5. Paul Lalli Guest

    On Mar 1, 8:15 am, ""

    > 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
     
    Paul Lalli, Mar 1, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amratash
    Replies:
    0
    Views:
    529
    Amratash
    Apr 13, 2004
  2. unomystEz
    Replies:
    0
    Views:
    557
    unomystEz
    Nov 19, 2006
  3. Joost Kraaijeveld

    How to parse this postgresql log file?

    Joost Kraaijeveld, Dec 8, 2006, in forum: Java
    Replies:
    3
    Views:
    757
    Martin Gregorie
    Dec 9, 2006
  4. Parse a log file

    , Jan 18, 2010, in forum: Python
    Replies:
    3
    Views:
    316
  5. Max Adams
    Replies:
    4
    Views:
    108
    Tad McClellan
    Aug 29, 2003
Loading...

Share This Page