Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > grep using regular expression

Reply
Thread Tools

grep using regular expression

 
 
Kimi
Guest
Posts: n/a
 
      12-05-2006
Hi,

I am comparitively new to perl and so facing few issues with the
syntax. I have a log file whose contents are similar to

[Fri Nov 24 06:22:45 2006] Started instance Robo:4001
[Fri Nov 24 06:22:45 2006] Instance Robo:4002 is not running
[Sat Nov 25 06:22:46 2006] Started instance Robo:4002
[Sat Nov 25 06:22:46 2006] Instance Robo:4003 is not running
[Mon Nov 28 06:22:46 2006] Started instance Robo:4003
[Mon Nov 28 06:27:46 2006] Instance Robo:4000 is not running
[Wed Nov 30 06:27:46 2006] Started instance Robo:4000
[Wed Nov 30 06:29:46 2006] Instance Robo:4000 is not running

I am trying to extract the contents which holds true for the following
criteria

1. has the text "Started"
2. Date as specified in the regular expression

I have some general idea about regular expression, so i know that the
date in the above content can be matched using

/([A-Za-z]+)\s+([A-Za-z]+)\s+([0-9]+)\s+([0-9]+)[0-9]+)[0-9]+)\s+([0-9]+)/

But i am not clear how it can be used for the following case with perl
syntax.

I also would like to know if it is possible to use variable name for
pattern matching, say $day = Fri, being a variable $day will hold any
of the values between ( Mon, Tue, Wed, Fri, etc.. )

Can $day be used in regular expression?????

Any help is appreciable

regards,
Kimi

 
Reply With Quote
 
 
 
 
Kimi
Guest
Posts: n/a
 
      12-05-2006

A. Sinan Unur wrote:
> "Kimi" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed) ups.com:
>
> > [Fri Nov 24 06:22:45 2006] Started instance Robo:4001
> > [Fri Nov 24 06:22:45 2006] Instance Robo:4002 is not running
> > [Sat Nov 25 06:22:46 2006] Started instance Robo:4002
> > [Sat Nov 25 06:22:46 2006] Instance Robo:4003 is not running
> > [Mon Nov 28 06:22:46 2006] Started instance Robo:4003
> > [Mon Nov 28 06:27:46 2006] Instance Robo:4000 is not running
> > [Wed Nov 30 06:27:46 2006] Started instance Robo:4000
> > [Wed Nov 30 06:29:46 2006] Instance Robo:4000 is not running
> >
> > I am trying to extract the contents which holds true for the following
> > criteria
> >
> > 1. has the text "Started"
> > 2. Date as specified in the regular expression

>
> No need to make this overly complicated:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $date_re = 'Nov 30.+2006';
>
> while (<DATA>) {
> next unless s/^\[//;
> my ($date, $msg) = split /\] /;
> print if $msg =~ /^Started/ and $date =~ /$date_re/;
> }
>
>
> __DATA__
> [Fri Nov 24 06:22:45 2006] Started instance Robo:4001
> [Fri Nov 24 06:22:45 2006] Instance Robo:4002 is not running
> [Sat Nov 25 06:22:46 2006] Started instance Robo:4002
> [Sat Nov 25 06:22:46 2006] Instance Robo:4003 is not running
> [Mon Nov 28 06:22:46 2006] Started instance Robo:4003
> [Mon Nov 28 06:27:46 2006] Instance Robo:4000 is not running
> [Wed Nov 30 06:27:46 2006] Started instance Robo:4000
> [Wed Nov 30 06:29:46 2006] Instance Robo:4000 is not running
>
> C:\DOCUME~1\asu1\LOCALS~1\Temp> s
> Wed Nov 30 06:27:46 2006] Started instance Robo:4000
> --
> A. Sinan Unur <(E-Mail Removed)>
> (remove .invalid and reverse each component for email address)
>
> comp.lang.perl.misc guidelines on the WWW:
> http://augustmail.com/~tadmc/clpmisc...uidelines.html



Thanks sinan...

it works well...

It would be grateful if I can get to know how to use a variable instead
of DATA.

regards,
Kimi

 
Reply With Quote
 
 
 
 
Craig
Guest
Posts: n/a
 
      12-05-2006
> > while (<DATA>) {
> > next unless s/^\[//;
> > my ($date, $msg) = split /\] /;
> > print if $msg =~ /^Started/ and $date =~ /$date_re/;
> > }


my $file = "..."; # your path and filename
my $fh;
open($fh, $file) or die("Can't open $file: $!");
while (<$fh> ) {
next unless s/^\[//;
my ($date, $msg) = split /\] /;
print if $msg =~ /^Started/ and $date =~ /$date_re/;
}

 
Reply With Quote
 
Kimi
Guest
Posts: n/a
 
      12-06-2006


On Dec 6, 12:10 am, "Craig" <(E-Mail Removed)> wrote:
> > > while (<DATA>) {
> > > next unless s/^\[//;
> > > my ($date, $msg) = split /\] /;
> > > print if $msg =~ /^Started/ and $date =~ /$date_re/;
> > > }my $file = "..."; # your path and filename

> my $fh;
> open($fh, $file) or die("Can't open $file: $!");
> while (<$fh> ) {
> next unless s/^\[//;
> my ($date, $msg) = split /\] /;
> print if $msg =~ /^Started/ and $date =~ /$date_re/;
>
> }


Thank you... that helped a lot...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re.search much slower then grep on some regular expressions Henning_Thornblad Python 46 07-10-2008 05:15 PM
Anyway to make grep take a regular string? Feng Tien Ruby 3 11-16-2007 08:32 AM
regular expression syntax the same in Python, Perl and grep? seberino@spawar.navy.mil Python 3 11-08-2007 12:09 AM
regular expression for perl, tcl, sed, grep, awk Jay eL Perl Misc 2 12-09-2003 04:23 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments