473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

pattern matching with time

Hi All,

Below is the contents of a log file.I need to collect the time when LP is disabled.
ie( 07:12:29.69) and the time LP is enabled(07:13:57.08),and find the difference betwwen two.Now I can match the word "Com: LP is Disabled" in the file but
how to take that particular time which is six lines above it.

Note:We cannot make any comparison using lp/2 as it keeps on changing.

can any one tell me how to do this?


contents of file::


Lp/2; 2007-01-03 07:12:29.69
SET critical processing underlyingResourceUnavail 70120200
ADMIN: unlocked OPER: disabled USAGE: idle
AVAIL: dependency PROC: CNTRL:
ALARM: critical STBY: notSet UNKNW: false
Id: C8 Rel: Shelf Card/2
Com: LP is Disabled
Int: 0/0/2/11244; pcsLpMgr.cc; 4385;


Shelf Card/2; 2007-01-03 07:13:22.02
CLR cleared equipment processorProblem 70120100
ADMIN: unlocked OPER: enabled USAGE: idle
AVAIL: PROC: CNTRL:
ALARM: STBY: notSet UNKNW: false
Id: CB Rel:
Com: Card is Enabled
Int: 0/0/2/11244; pcsCardMgr.cc; 9849;



114>
Lp/2; 2007-01-03 07:13:57.08
CLR cleared processing underlyingResourceUnavail 00000000
ADMIN: unlocked OPER: enabled USAGE: active
AVAIL: PROC: CNTRL:
ALARM: STBY: providingServ UNKNW: false
Id: CD Rel: Shelf Card/2
Com: LP is Enabled
Int: 0/0/2/11244; pcsLpMgr.cc; 4385; U
Feb 9 '07 #1
4 1359
KevinADC
4,059 Expert 2GB
collect the time first then check if it's disable or enable.
Feb 9 '07 #2
KevinADC
4,059 Expert 2GB
or might be easier to read the file backwards. Then you get your enabled/disabled tokens first. Can you install perl modules? There is a module for reading files backwards.

http://search.cpan.org/~uri/File-Rea...adBackwards.pm

if not and the file isn't gigantic you can read it into an array and reverse the array.
Feb 9 '07 #3
@kriz4321
Expand|Select|Wrap|Line Numbers
  1. @file = (
  2.   "Lp/2; 2007-01-03 07:12:29.69\n",
  3.   "SET critical processing underlyingResourceUnavail 70120200\n",
  4.   "ADMIN: unlocked OPER: disabled USAGE: idle\n",
  5.   "AVAIL: dependency PROC: CNTRL:\n",
  6.   "ALARM: critical STBY: notSet UNKNW: false\n",
  7.   "Id: C8 Rel: Shelf Card/2\n",
  8.   "Com: LP is Disabled\n",
  9.   "Int: 0/0/2/11244; pcsLpMgr.cc; 4385;\n",
  10.   "\n",
  11.   "\n",
  12.   "Shelf Card/2; 2007-01-03 07:13:22.02\n",
  13.   "CLR cleared equipment processorProblem 70120100\n",
  14.   "ADMIN: unlocked OPER: enabled USAGE: idle\n",
  15.   "AVAIL: PROC: CNTRL:\n",
  16.   "ALARM: STBY: notSet UNKNW: false\n",
  17.   "Id: CB Rel:\n",
  18.   "Com: Card is Enabled\n",
  19.   "Int: 0/0/2/11244; pcsCardMgr.cc; 9849;\n",
  20.   "\n",
  21.   "\n",
  22.   "\n",
  23.   "114>\n",
  24.   "Lp/2; 2007-01-03 07:13:57.08\n",
  25.   "CLR cleared processing underlyingResourceUnavail 00000000\n",
  26.   "ADMIN: unlocked OPER: enabled USAGE: active\n",
  27.   "AVAIL: PROC: CNTRL:\n",
  28.   "ALARM: STBY: providingServ UNKNW: false\n",
  29.   "Id: CD Rel: Shelf Card/2\n",
  30.   "Com: LP is Enabled\n",
  31.   "Int: 0/0/2/11244; pcsLpMgr.cc; 4385; U\n",
  32. );
  33.  
  34. sub printState {
  35.   my($whichTime, $whichLp, $state) = @_;
  36.  
  37.   printf("[$whichTime] Lp$whichLp -> $state\n");
  38. }
  39.  
  40. for $line ( @file ) {
  41.   $state = "";
  42.  
  43.   if ( $line =~ /^Lp/ ) {
  44.     ($whichLp, $whichTime) = ($line =~ /^^Lp\/([\d+]);\s+[\d-]+\s+(.*)/g);
  45.   }
  46.   elsif ( $line =~ /^ADMIN.*enabled\s+USAGE:\s+active.*/ ) {
  47.     printState($whichTime, $whichLp, "enabled");
  48.   }
  49.   elsif ( $line =~ /^ADMIN.*disabled\s+USAGE:\s+idle.*/ ) {
  50.     printState($whichTime, $whichLp, "disabled");
  51.   }
  52. }
Substitute the code
Expand|Select|Wrap|Line Numbers
  1. @file = ...
by your input method of reading the file.

Greetz, Doc
Feb 9 '07 #4
KevinADC
4,059 Expert 2GB
here is another suggestion:

Expand|Select|Wrap|Line Numbers
  1. my @log = ();
  2. open(FH,'log.txt') or die "$!";
  3. while(<FH>){
  4.    push @log,$1 if (m|^Lp/2;\s+\S+\s+(\S+)$|);
  5.    push @log,$1 if (m/^Com: LP is (Enabled|Disabled)/);
  6. }
  7. close(FH);
  8. @log = reverse @log;
  9.  
  10. my ($e,$d) =(0,0);
  11. foreach my $i (0..$#log-1) {
  12.    if ($log[$i] eq 'Enabled'){
  13.       $e = $log[$i+1];
  14.    }
  15.    elsif ($log[$i] eq 'Disabled') {
  16.       $d = $log[$i+1];
  17.    }
  18.    if ($e && $d) {
  19.       convert them into total seconds
  20.       subtract to get difference
  21.       reset $e and $d if there will be more
  22.    }
  23. }
admitedly, I did not put much thought into this, so it probably can be improved upon.
Feb 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
3
by: Greg Lindstrom | last post by:
Hello- I'm running Python 2.2.3 on Windows XP "Professional" and am reading a file wit 1 very long line of text (the line consists of multiple records with no cr/lf). What I would like to do is...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
1
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
9
by: Jim Lewis | last post by:
Anyone have experience with string pattern matching? I need a fast way to match variables to strings. Example: string - variables ============ abcaaab - xyz abca - xy eeabcac - vxw x...
2
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and...
5
by: pramodkh | last post by:
Hi All I am trying to match a pattern in a file and insert a line. If the pattern matches then insert a line before the matching pattern line. for example, I have the following content in a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.