grep using regular expression

K

Kimi

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
 
K

Kimi

A. Sinan Unur said:
[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 <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.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
 
C

Craig

while ( said:
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/;
}
 
K

Kimi

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top