Solved

# Compare certain lines and determine the difference in time

Posted on 2004-10-22
Medium Priority
204 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
• 11
• 11
22 Comments

LVL 85

Expert Comment

ID: 12379555
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

ID: 12379692
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

ID: 12379801
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 85

Expert Comment

ID: 12379833
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 85

Expert Comment

ID: 12379849
Yes, the 1 second delay is from the +1
0

Author Comment

ID: 12379892
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 85

Expert Comment

ID: 12379898
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 85

Expert Comment

ID: 12379910
print \$_;
0

LVL 85

Expert Comment

ID: 12379964
#or just
print;
0

Author Comment

ID: 12379984
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 85

Expert Comment

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

Author Comment

ID: 12381440
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 85

Expert Comment

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

Author Comment

ID: 12398599
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

ID: 12398639
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 85

Expert Comment

ID: 12398725
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

ID: 12399180
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

ID: 12400460
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 85

Expert Comment

ID: 12401189
Did you include the
\$start{\$10} &&
?
0

Author Comment

ID: 12401724
yes, and to work around that I added this to my sub

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

LVL 85

Accepted Solution

ozo earned 2000 total points
ID: 12404114
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

ID: 12408928
Opps.... typo on my part... Thanks so much ozo.. !!!!
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Join & Write a Comment Already a member? Login.

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…
Six Sigma Control Plans
###### Suggested Courses
Course of the Month10 days, 10 hours left to enroll

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

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