Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts

    How to count/group errors by Year/Month/Week?

    Hi,
    I got error log something like this:
    Time: 2010 11 03 21:55:23
    ErrorCode: 1x000987654
    Message: YYYYYYYYY

    Time: 2010 11 04 20:36:45
    ErrorCode: 1x000123456
    Message: OOOOOOOO

    Time: 2010 11 05 17:32:10
    ErrorCode: 1x00034987
    Message: NNNNNNN

    Time: 2011 01 02 13:28:14
    ErrorCode: 1x00022334476
    Message: KKKKKKKKKK

    I want to make a script that would read the log, count errors for each Year/Month/Week. I just have no idea how to count and then group different errors by Year/Month/Week. Could you give me an idea how to do that?
    Thanks, vl123456

  • #2
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Something like the following:

    Code:
    use Data::Dumper;
    use DateTime;
    
    use strict;
    use warnings;
    
    $/ = "\n\n"; # Paragraph mode
    
    my %count_year;
    my %count_month;
    my %count_week;
    
    while (<DATA>) {
    	if (/Time: (\d{4}) (\d\d) (\d\d)/) {
    		my $dt = DateTime->new(
    			year   => $1,
    			month  => $2,
    			day    => $3,
    		);
    		
    		$count_year{$dt->strftime("%Y")}++;
    		$count_month{$dt->strftime("%Y%m")}++;
    		$count_week{$dt->strftime("%Y%W")}++;
    		
    	} else {
    		warn "No date/time found in error: $_";
    	}
    }
    
    print Dumper(\%count_year);
    print Dumper(\%count_month);
    print Dumper(\%count_week);
    
    __DATA__
    Time: 2010 11 03 21:55:23
    ErrorCode: 1x000987654 
    Message: YYYYYYYYY
    
    Time: 2010 11 04 20:36:45
    ErrorCode: 1x000123456 
    Message: OOOOOOOO
    
    Time: 2010 11 05 17:32:10
    ErrorCode: 1x00034987 
    Message: NNNNNNN
    
    Time: 2011 01 02 13:28:14
    ErrorCode: 1x00022334476 
    Message: KKKKKKKKKK

  • Users who have thanked miller for this post:

    vl123456 (04-23-2011)

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Thank you Miller!
    I cannot try your script. I got error :

    Can't locate DateTime.pm in @INC (@INC contains: C:/Perl64/site/lib C:/Perl64/li
    b .) at C:\PERL_TEST\Hash\2.pl line 4.
    BEGIN failed--compilation aborted at C:\PERL_TEST\Hash\2.pl line 4.

    I think I’m missing Date::Calc module. Have to search internet for solution.
    Thank you again!
    vl123456

  • #4
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    You need to learn how to install cpan modules, mate.

    If you're using Strawberry perl, then just do the following;

    Code:
    cpan -i DateTime
    If using ActivePerl, then I believe the following will work

    Code:
    ppm -i DateTime
    - Miller

  • Users who have thanked miller for this post:

    vl123456 (04-23-2011)

  • #5
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Where I should enter <ppm -i DateTime>?

    Thanks, vl123456

  • #6
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by vl123456 View Post
    Where I should enter <ppm -i DateTime>?
    At the command prompt.

    If you have ActivePerl, just read the website on how to install modules.

    - M
    Last edited by miller; 04-23-2011 at 07:07 AM.

  • Users who have thanked miller for this post:

    vl123456 (04-23-2011)

  • #7
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    ppm -i DateTime produced an error:
    C:\Windows\system32>ppm -i DateTime
    Unrecognized ppm command '-i'; try one of area, config, describe, files,
    gui, help, info, install, list, log, profile, query, remove, repo,
    search, tree, uninstall, update, upgrade, verify, or version
    Usage: ppm <cmd> <arg>...
    Run 'ppm help' to learn more.

    I ran ppm.bat and the module seems to be installed -version 6.3.
    Thanks for help.
    Vl123456

  • #8
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Hi, I still cannot run the scrip because of the error <Datetime Module>. Using ppm.bat I installed the module but error still there. I tried to install strawberry Perl but had an error during installation. I was wondering if you guys know a good website with the instruction on the problem. Does anybody knows why during Active Perl installation I cannot check/uncheck “Create.pl script mapping fro Perl”?
    Thanks, vl123456

  • #9
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by vl123456 View Post
    ppm -i DateTime produced an error:
    C:\Windows\system32>ppm -i DateTime
    Unrecognized ppm command '-i'; try one of area, config, describe, files,
    gui, help, info, install, list, log, profile, query, remove, repo,
    search, tree, uninstall, update, upgrade, verify, or version
    Usage: ppm <cmd> <arg>...
    Run 'ppm help' to learn more.

    I ran ppm.bat and the module seems to be installed -version 6.3.
    Thanks for help.
    Vl123456
    The proper command for activestate is

    Code:
    ppm install DateTime

  • Users who have thanked miller for this post:

    vl123456 (05-28-2011)

  • #10
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Thank you Miller!
    I installed the module and it is working now.
    vl123456


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •