Want to use equivalent of grep function in perl

P

perlmbk

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
}
 
J

John Bokma

Jim Gibson said:
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: $!";
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: $!";
 
I

it_says_BALLS_on_your_forehead

perlmbk said:
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
 
Q

quetzalcotl

perlmbk said:
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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top