Want to use equivalent of grep function in perl

Discussion in 'Perl Misc' started by perlmbk, Nov 10, 2005.

  1. perlmbk

    perlmbk Guest

    Hi,

    I want to grep the alert message ( $msg ) for string patterns contained
    in the file alertpager.txt, here is the code :
    #!/usr/bin/perl
    local $nmsg = $ENV{'NOTIFICATION_MSG'};
    $nmsg = $ARGV[2] unless $nmsg;


    local $matrixpager_file = "c:\\alertpager.txt";

    open(MATRIXPAGER, $matrixpager_file);
    local @content = <MATRIXPAGER>;
    close(MATRIXPAGER);

    foreach $ln (@content)
    {
    if ($ln = $msg) <----- want to grep the
    "$ln" value here
    {
    # if success, echo hello
    # else exit
    }
     
    perlmbk, Nov 10, 2005
    #1
    1. Advertising

  2. perlmbk

    John Bokma Guest

    Jim Gibson <> wrote:

    > You should test to see if the open worked:
    >
    > open(MATRIXPAGER, $matrixpager_file) or
    > die("Can't open $matrixpager_file: $!");


    Smells like PHP :)

    open my $fh, $matrixpager_file
    or die "Can't open '$matrixpager_file' for reading: $!";

    >> local @content = <MATRIXPAGER>;
    >> close(MATRIXPAGER);

    >
    > chomp(@content);
    >
    >>
    >> foreach $ln (@content)
    >> {
    >> if ($ln = $msg) <----- want to grep the
    >> "$ln" value here

    >
    > if( $nmsg =~ /$ln/ ) {
    > print "hello\n";
    > last;
    > }


    while ( my $line = <$fh> ) {

    index( $line, $nmsg ) >= 0 or next;
    print "found at line $.\n";
    last;
    }

    close $fh
    or die "Can't close '$matrixpager_file' after reading: $!";



    >
    >> {
    >> # if success, echo hello
    >> # else exit
    >> }
    >>

    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com
    >




    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Nov 10, 2005
    #2
    1. Advertising

  3. perlmbk wrote:
    > Hi,
    >
    > I want to grep the alert message ( $msg ) for string patterns contained
    > in the file alertpager.txt, here is the code :

    ....
    > foreach $ln (@content)
    > {
    > if ($ln = $msg) <----- want to grep the
    > "$ln" value here
    > {
    > # if success, echo hello
    > # else exit
    > }


    my @matches = grep {$_ eq $msg} @content;

    or, since you mentioned 'string patterns contained'...

    my @matches = grep {/$msg/o} @content;

    then you can check the size of @matches to see if there were any
    successful matches.

    HTH
     
    it_says_BALLS_on_your_forehead, Nov 10, 2005
    #3
  4. perlmbk

    Guest

    perlmbk schrieb:

    > local $matrixpager_file = "c:\\alertpager.txt";


    my $matrixpager_file = "c:/alertpager.txt";

    Get used to use "/" as path separator, you may want to port your script
    to UNIX or LINUX in the future.
     
    , Nov 10, 2005
    #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. Al Belden

    perl vs Unix grep

    Al Belden, Jul 3, 2004, in forum: Perl
    Replies:
    1
    Views:
    5,265
    Giridhar Nandigam
    Jul 7, 2004
  2. js
    Replies:
    4
    Views:
    388
    Fredrik Lundh
    Jan 3, 2007
  3. Tim Smith
    Replies:
    1
    Views:
    380
    Marc 'BlackJack' Rintsch
    Dec 29, 2006
  4. tereglow

    Grep Equivalent for Python

    tereglow, Mar 14, 2007, in forum: Python
    Replies:
    15
    Views:
    26,821
    tereglow
    Mar 19, 2007
  5. Replies:
    3
    Views:
    407
    BartlebyScrivener
    Nov 8, 2007
Loading...

Share This Page