Solved

Compare certain lines and determine the difference in time

Posted on 2004-10-22
157 Views
Last Modified: 2010-03-05


I have a large file that I need to periodically grep though for certain field. To ensure the transactions are occurring in a timely matter.

The begging of a transaction is marked with “req  > 210 :”    and the end of a transaction is marked with “** Status  :”   The key to line up the correct beginning transaction to the correct ending transaction is random number, is maintains the same number through the transaction.  Ie



[18/Aug/2004:11:45:00 -0500][24-I] req  > 210 :
[18/Aug/2004:11:45:03 -0500][24-I] ** Status  :


The key here would be 24-I.  There would be other transaction lines in between the these two lines. Because the transaction number may be reused I need to match the first pair, than find the difference in seconds (this case 3) and do X if greater then 1 second.  

Any thoughts
0
Question by:MatthewF
    22 Comments
     
    LVL 84

    Expert Comment

    by:ozo
    use Time::Local 'timegm_nocheck';
    @M{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)}=(0..11);
    while( <> ){
      $start{$10} = timegm_nocheck $6,$5+($7.$9),$4+($7.$8),$1,$M{$2},$3 if /\[(\d+)\/(\w+)\/(\d+):(\d+):(\d+):(\d+)\s*([+-]?)(\d\d)(\d\d)\]\[(.*?)\]\s+req\s+>\s+210\s+:/ ;
      if( /\[(\d+)\/(\w+)\/(\d+):(\d+):(\d+):(\d+)\s*([+-]?)(\d\d)(\d\d)\]\[(.*?)\]\s+\*\*\s+Status\s+:/
       && $start{$10}+1 < timegm_nocheck $6,$5+($7.$9),$4+($7.$8),$1,$M{$2},$3
      ){
            &X;
      }
    }

    0
     

    Author Comment

    by:MatthewF
    Im a bit confused so im my input file (called log.ot) looked like:

    I see the sub for doing x,  where would I tail the file to two diffrect arrays or would i?

    This would be @start array
    [18/Aug/2004:11:45:00 -0500][24-I] req  > 210 :
    [18/Aug/2004:11:45:01 -0500][27-I] req  > 210 :
    [18/Aug/2004:11:45:02 -0500][28-I] req  > 210 :
    [18/Aug/2004:11:45:03 -0500][29-I] req  > 210 :


    This would be @finish array
    [18/Aug/2004:11:45:03 -0500][24-I] ** Status  :
    [18/Aug/2004:11:45:03 -0500][27-I] ** Status  :
    [18/Aug/2004:11:45:03 -0500][28-I] ** Status  :
    [18/Aug/2004:11:45:03 -0500][29-I] ** Status  :


    0
     

    Author Comment

    by:MatthewF
    I put the following in

    $all_host="log.ot";
    open(HOST,"<$all_host") || die "Can't open infile: $!\n";
    while( <HOST> ){

    and x looks good.  I gther the one second delay is from :

    && $start{$10}+1 < timegm_nocheck $6,$5+($7.$9),$4+($7.$8),$1,$M{$2},$3
         the plus one correct?
    0
     
    LVL 84

    Expert Comment

    by:ozo
    There is only a %start hash, which is filled when it sees " req  > 210 :"
    and examined when it sees " ** Status  :"

    You would invoke the program as:
    perl script.perl < log.ot
    0
     
    LVL 84

    Expert Comment

    by:ozo
    Yes, the 1 second delay is from the +1
    0
     

    Author Comment

    by:MatthewF
    last question. If the seconds are greater then one, how could I print out the original status: lie

    [18/Aug/2004:11:45:03 -0500][24-I] ** Status  
    0
     
    LVL 84

    Expert Comment

    by:ozo
    the +($7.$9) and +($7.$8) might be -($7.$9) and -($7.$8)
    I forget whether the -0500 was an offset from GMT or to GMT
    0
     
    LVL 84

    Expert Comment

    by:ozo
    print $_;
    0
     
    LVL 84

    Expert Comment

    by:ozo
    #or just
      print;
    0
     

    Author Comment

    by:MatthewF
    When I did a print on sub x I get

    sub x
    print "time $6,$5+($7.$9),$4+($7.$8) is  out of spec\n";

    The return I get is:

    time 47,48+(-.00),08+(-.05) is  out of spec

    I was wanting to get the initial line, is thta possible?
    0
     
    LVL 84

    Expert Comment

    by:ozo
    sub x {
        print "time ",/\[(.*?)\]/," is out of spec\n";
    }
    0
     

    Author Comment

    by:MatthewF
    If this is too difficult let me know. But I want to do was to compare a req  > 210 :with the first Status line with the same transaction number. It may be the case there is not a corresponding Status line and if not do nothing


    [18/Aug/2004:11:44:50 -0500][57-I] req  > 210 :      REQ
    [18/Aug/2004:11:44:50 -0500][23-I] req  > 210 :
    [18/Aug/2004:11:44:51 -0500][31-I] req  > 210 :
    [18/Aug/2004:11:44:53 -0500][24-I] ** Status  :.
    [18/Aug/2004:11:44:56 -0500][24-I] ** Status  : '.
    [18/Aug/2004:11:44:56 -0500][27-I] ** Status  : .
    [18/Aug/2004:11:44:56 -0500][65-I] ** Status  : '.
    [18/Aug/2004:11:44:56 -0500][24-I] ** Status  : '.
    [18/Aug/2004:11:44:56 -0500][23-I] ** Status  : .
    [18/Aug/2004:11:44:58 -0500][24-I] ** Status  : '
    [18/Aug/2004:11:44:58 -0500][31-I] ** Status  : '.
    [18/Aug/2004:11:44:58 -0500][57-I] ** Status  : '.   Correspoding Status line
    [18/Aug/2004:11:44:58 -0500][27-I] ** Status  : .
    [18/Aug/2004:11:45:00 -0500][23-I] req  > 210 :
    [18/Aug/2004:11:45:00 -0500][27-I] req  > 210 :
    [18/Aug/2004:11:45:00 -0500][57-I] ** Status  :
    [18/Aug/2004:11:45:00 -0500][24-I] req  > 210 :
    [18/Aug/2004:11:45:03 -0500][23-I] ** Status  :  
    [18/Aug/2004:11:45:05 -0500][57-I] ** Status  :
    [18/Aug/2004:11:45:05 -0500][24-I] ** Status  :
    [18/Aug/2004:11:45:05 -0500][65-I] ** Status  :
    [18/Aug/2004:11:45:06 -0500][24-I] ** Status  :
    [18/Aug/2004:11:45:06 -0500][31-I] ** Status  :
    [18/Aug/2004:11:45:06 -0500][27-I] ** Status  :
    [18/Aug/2004:11:45:06 -0500][23-I] ** Status  :
    [18/Aug/2004:11:45:07 -0500][57-I] ** Status  :
    [18/Aug/2004:11:45:08 -0500][23-I] req  > 210 : '
    0
     
    LVL 84

    Expert Comment

    by:ozo
    The program I gave should do exactly that.
    Are you encountering a problem?
    0
     

    Author Comment

    by:MatthewF
    When I run the script against the above input I would except no more the 7 liens of ouput form sub x becuase there are only 7 lines with req  > 210 :  However I receieved 20 "out of spec lines"
    0
     

    Author Comment

    by:MatthewF
    I think part of the issue is the compare need to stop on the first match

    [18/Aug/2004:11:44:50 -0500][23-I] req  > 210 :
    [18/Aug/2004:11:44:51 -0500][23-I] ** Status  :     Stop here..........
    [18/Aug/2004:11:45:03 -0500][23-I] ** Status  :
    0
     
    LVL 84

    Expert Comment

    by:ozo
    Sorry, I didn't realize a second
    [23-I] ** Status  :
    line could occur without a second
    [23-I] req  > 210 :
    line

      if( /\[(\d+)\/(\w+)\/(\d+):(\d+):(\d+):(\d+)\s*([+-]?)(\d\d)(\d\d)\]\[(.*?)\]\s+\*\*\s+Status\s+:/
       && $start{$10} && $start{$10}+1 < timegm_nocheck $6,$5+($7.$9),$4+($7.$8),$1,$M{$2},$3
      ){
            &x;
            delete $start{$10};
      }
    }
    0
     

    Author Comment

    by:MatthewF
    I still get mutiole out  I hope this input with comments may help




    [18/Aug/2004:11:44:50 -0500][57-I] req  > 210 :      REQ
    [18/Aug/2004:11:44:50 -0500][23-I] req  > 210 :
    [18/Aug/2004:11:44:51 -0500][31-I] req  > 210 :
    [18/Aug/2004:11:44:53 -0500][24-I] ** Status  :.   no prior reg > 210  for 24-I ..skip
    [18/Aug/2004:11:44:56 -0500][24-I] ** Status  : '.  no prior reg > 210  for 24-I ..skip
    [18/Aug/2004:11:44:56 -0500][23-I] ** Status  : .   Correspoding Status line  for 23-I   ---Matched gt then 1
    [18/Aug/2004:11:44:58 -0500][31-I] ** Status  : '.  Correspoding Status line    for 31 -I ----Matched gt then 1
    [18/Aug/2004:11:44:58 -0500][57-I] ** Status  : '.   Correspoding Status line  for 57 -I ---Matched gt then 1
    [18/Aug/2004:11:44:58 -0500][57-I] ** Status  : .    matched already,,skip





    [
    0
     

    Author Comment

    by:MatthewF
    When I ran the script I get the following output from sub x. Note the the  print "$start{$10}\n\n" works on the three correct matches and the other three lines print only $_.  I would have not espected the lines with only $_  not to be printed

    sub x {
     print "$_";
     print "$start{$10}\n\n";
     }      

    [18/Aug/2004:11:44:53 -0500][24-I] ** Status :. no prior reg > 210 for 24-I ..skip


    [18/Aug/2004:11:44:56 -0500][24-I] ** Status : '. no prior reg > 210 for 24-I ..skip


    [18/Aug/2004:11:44:56 -0500][23-I] ** Status : . Correspoding Status line for 23-I ---Matched gt then 1
    1092811490

    [18/Aug/2004:11:44:58 -0500][31-I] ** Status : '. Correspoding Status line for 31 -I ----Matched gt then 1
    1092811491

    [18/Aug/2004:11:44:58 -0500][57-I] ** Status : '. Correspoding Status line for 57 -I ---Matched gt then 1
    1092811490

    [18/Aug/2004:11:44:58 -0500][57-I] ** Status : . matched already,,skip
    0
     
    LVL 84

    Expert Comment

    by:ozo
    Did you include the
    $start{$10} &&
    ?
    0
     

    Author Comment

    by:MatthewF
    yes, and to work around that I added this to my sub


    if  ($start{$10} > 0)
    {
     print "$_";
     delete $start{$10};
     }}      
    0
     
    LVL 84

    Accepted Solution

    by:
    I had that in the condition before the sub

      if( /\[(\d+)\/(\w+)\/(\d+):(\d+):(\d+):(\d+)\s*([+-]?)(\d\d)(\d\d)\]\[(.*?)\]\s+\*\*\s+Status\s+:/
       && $start{$10} && $start{$10}+1 < timegm_nocheck $6,$5+($7.$9),$4+($7.$8),$1,$M{$2},$3
      ){
    0
     

    Author Comment

    by:MatthewF
    Opps.... typo on my part... Thanks so much ozo.. !!!!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now