grep using regular expression

Discussion in 'Perl Misc' started by Kimi, Dec 5, 2006.

  1. Kimi

    Kimi Guest

    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
    Kimi, Dec 5, 2006
    #1
    1. Advertising

  2. Kimi

    Kimi Guest

    A. Sinan Unur wrote:
    > "Kimi" <> wrote in
    > news::
    >
    > > [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 <>
    > (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
    Kimi, Dec 5, 2006
    #2
    1. Advertising

  3. Kimi

    Craig Guest

    > > 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/;
    }
    Craig, Dec 5, 2006
    #3
  4. Kimi

    Kimi Guest

    On Dec 6, 12:10 am, "Craig" <> 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...
    Kimi, Dec 6, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,267
  2. Replies:
    3
    Views:
    375
    BartlebyScrivener
    Nov 8, 2007
  3. Henning_Thornblad
    Replies:
    46
    Views:
    1,005
    Kris Kennaway
    Jul 10, 2008
  4. Feng Tien
    Replies:
    3
    Views:
    90
    Alex Gutteridge
    Nov 16, 2007
  5. Jay eL
    Replies:
    2
    Views:
    188
    James Willmore
    Dec 9, 2003
Loading...

Share This Page