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 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Converting Time from GTM

    Greetings!
    I got a log file (data.log), it goes something like this:
    Time: Thu Mar 17 20:11:47 2011 GMT
    Error: 0BA01007A
    Message: Test failed to load failed.
    Error: 0C1701001D
    Message: Parse error found.
    Time: Thu Mar 17 20:12:22 2011 GMT
    Error: 0DA01007A
    Message: Test failed to load failed.
    Error: 0PP1701001D
    Message: Parse error found.
    Time: Thu Mar 17 21:36:24 2011 GMT
    ======================================================
    I want to create a report from the file but first I want to convert the Time from GTM to:
    03/17/2001 20:11:47
    I知 new to perl or any programming but I知 reading a lot about perl lately I become familiar with the code (to some extant). Could you help me with the conversion?
    Thank you.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by vl123456 View Post
    Greetings!
    I got a log file (data.log), it goes something like this:
    Time: Thu Mar 17 20:11:47 2011 GMT
    Error: 0BA01007A
    Message: Test failed to load failed.
    Error: 0C1701001D
    Message: Parse error found.
    Time: Thu Mar 17 20:12:22 2011 GMT
    Error: 0DA01007A
    Message: Test failed to load failed.
    Error: 0PP1701001D
    Message: Parse error found.
    Time: Thu Mar 17 21:36:24 2011 GMT
    ======================================================
    I want to create a report from the file but first I want to convert the Time from GTM to:
    03/17/2001 20:11:47
    I知 new to perl or any programming but I知 reading a lot about perl lately I become familiar with the code (to some extant). Could you help me with the conversion?
    Thank you.
    as a side note, if you don't use a timezone in output, default will be gmt, else is hard to guess, .

    look to the DateTime::Format::Strptime module, can be used for parsing the datetime and conversion.

    best regards

  • Users who have thanked oesxyl for this post:

    vl123456 (04-05-2011)

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    I’m just going to loop the file line by line and will replace the month to this:
    Dec to 12
    Jan to 01
    Feb to 03 and so on…
    Will remove all the days of the week then will split the line and join the year format (2011) with the (01 or o2 or 02 whatever…). I would get the line like this:
    03 17 2011 10:22:56
    Please advise if you have better way. I’m not sure if my why is the best way. You guy are the Pros here.
    Thanks, vl123456

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by vl123456 View Post
    I知 just going to loop the file line by line and will replace the month to this:
    Dec to 12
    Jan to 01
    Feb to 03 and so on
    Will remove all the days of the week then will split the line and join the year format (2011) with the (01 or o2 or 02 whatever). I would get the line like this:
    03 17 2011 10:22:56
    Please advise if you have better way. I知 not sure if my why is the best way. You guy are the Pros here.
    Thanks, vl123456
    first part 'Thu' is the day of the week, second is the month, that means 12 * 7 combination. Why do you want to replace it instead of using the DateTime module?

    http://search.cpan.org/~drolsky/Date...at/Strptime.pm

    best regards

  • Users who have thanked oesxyl for this post:

    vl123456 (04-05-2011)

  • #5
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Code:
    #!/usr/bin/perl
    
    use Date::Parse qw(strptime);
    use POSIX qw(strftime);
    
    while (<DATA>) {
    	if (/^Time:\s+(.*)/) {
    		# 03/17/2001 20:11:47
    		print strftime "%m/%d/%Y %H:%M:%S\n", strptime($1);
    	}
    }
    
    __DATA__
    Time: Thu Mar 17 20:11:47 2011 GMT
    Error: 0BA01007A
    Message: Test failed to load failed.
    Error: 0C1701001D
    Message: Parse error found.
    Time: Thu Mar 17 20:12:22 2011 GMT
    Error: 0DA01007A
    Message: Test failed to load failed.
    Error: 0PP1701001D
    Message: Parse error found.
    Time: Thu Mar 17 21:36:24 2011 GMT
    - Miller

  • Users who have thanked miller for this post:

    vl123456 (04-05-2011)

  • #6
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Thank you guys for helping me with this!
    I see I wasn’t very clear with what I exactly need.
    Here is the part of the log (<time> part of the log) :

    Time: Wed Nov 03 21:55:23 2010 GMT
    Time: Wed Nov 03 21:55:23 2010 GMT
    Time: Wed Nov 03 21:55:23 2010 GMT
    Time: Wed Nov 03 21:55:23 2010 GMT
    Time: Tue Mar 14 22:45:21 2011 GMT
    Time: Tue Mar 15 17:01:04 2011 GMT
    Time: Tue Mar 15 17:01:04 2011 GMT

    I wanted to modify it to be like this :
    Time: 2010 11 03 21:55:23
    Time: 2010 11 03 21:55:23
    Time: 2010 11 03 21:55:23
    Time: 2010 11 03 21:55:23
    Time: 2011 03 14 22:45:21
    Time: 2011 03 15 17:01:04
    Time: 2011 03 15 17:01:04
    I got the code to almost everything BUT moving the <year> to the beginning of the line where the <day) used to be. As you see the <year> is changing all the time and it is a problem for me.
    Here is the code I got :

    open(my $infile,"<","SM_Error.log") or die $!;
    open (my $outfile,">","mlogout.log") or die $!;
    while (<$infile>)
    {
    $_=~s/Mon//gi;
    $_=~s/Tue//gi;
    $_=~s/Wed//gi;
    $_=~s/Thu//gi;
    $_=~s/Fri//gi;
    $_=~s/Sut//gi;
    $_=~s/Sun//gi;

    $_=~s/GMT//gi;
    $_=~s/Jan/01:/gi;
    $_=~s/Feb/02:/gi;
    $_=~s/Mar/03:/gi;
    $_=~s/Apr/04:/gi;
    $_=~s/May/05:/gi;
    $_=~s/Jun/06:/gi;
    $_=~s/Jul/07:/gi;
    $_=~s/Aug/08:/gi;
    $_=~s/Sep/09:/gi;
    $_=~s/Oct/10:/gi;
    $_=~s/Nov/11:/gi;
    #$_=~s/Dec/12:/gi;
    print $_ ;
    print $outfile $_;
    }
    close $infile;
    close $outfile;

  • #7
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Code:
    #!/usr/bin/perl
    
    use Date::Parse qw(strptime);
    use POSIX qw(strftime);
    
    while (<DATA>) {
    	if (/^Time:\s+(.*)/) {
    		# 2001 03 17 20:11:47
    		print strftime "%Y %m %d %H:%M:%S\n", strptime($1);
    	}
    }
    
    __DATA__
    Time: Wed Nov 03 21:55:23 2010 GMT 
    Time: Wed Nov 03 21:55:23 2010 GMT 
    Time: Wed Nov 03 21:55:23 2010 GMT 
    Time: Wed Nov 03 21:55:23 2010 GMT
    Time: Tue Mar 14 22:45:21 2011 GMT 
    Time: Tue Mar 15 17:01:04 2011 GMT
    Time: Tue Mar 15 17:01:04 2011 GMT

  • Users who have thanked miller for this post:

    vl123456 (04-05-2011)

  • #8
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Thanks for your help again!

    I’m working on a Windows.

    When I tried to run the suggested code I got error
    C:\PERL_TEST\Reports\pp.pl
    Can't locate Date/Parse.pm in @INC (@INC contains: C:/Perl/site/lib C:/Perl/lib
    .) at C:\PERL_TEST\Reports\PP.pl line 8.
    BEGIN failed--compilation aborted at C:\PERL_TEST\Reports\PP.pl line 8.

    Here is the code I tried:

    #!/usr/bin/perl
    use Date::Parse qw(strptime);
    use POSIX qw(strftime);

    open(my $infile,"<","SM_Error.log") or die $!;
    open (my $outfile,">","mlogout.log") or die $!;

    while (<$infile>) {
    if (/^Time:\s+(.*)/) {
    # 2001 03 17 20:11:47
    print strftime "%Y %m %d %H:%M:%S\n", strptime($1);
    }
    }
    close $infile;
    close $outfile;

  • #9
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Are you using strawberry perl? If so, you just need to do the following command to install Date::Parse

    Code:
    cpan -i Date::Parse
    If not, then take a look at perldoc perlmodinstall.

  • Users who have thanked miller for this post:

    vl123456 (04-05-2011)


  •  

    Posting Permissions

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