简体   繁体   中英

grep command to count the alerts in perl

I have the below script:

    # Find any connection alerts to report
    for my $Alert (@Alerts)
    {   
        print " $Alert->{type} alerts:\n";

        print STDERR "Query: [\n$Alert->{query}]\n";
        my $alertCur = $dbh->prepare($Alert->{query})
          or die "Unable to prepare cursor for [$Alert->{query}]: $DBI::errstr\n";      

        $alertCur->execute() 
          or die " Unable to execute cursor: $DBI::errstr\n";

        while (my $rec = $alertCur->fetchrow_hashref())
        {               
            #already in alert, don't send
            next if (defined $rec->{alert_id});     

            print $fh "Location Alert Notification: $Alert->{type} for Site $rec->{location_id}  elapsedTime(D HH:MM):  $rec->{diff_tm}\n";
            print "Location Alert Notification: $Alert->{type} for Site $rec->{location_id}  elapsedTime(D HH:MM):  $rec->{diff_tm}\n";
            $dbh->do ("($Alert->{alert_id}, $rec->{location_id}, $rec->{imperial_site_id}, current)") or die "Unable to insert alert: $DBI::errstr\n";                                                          
            $alertCnt++;                                                                                                    
        }
    }

    $dbh->disconnect();

    if ($alertCnt > 0)
    {
        print "Found <$alertCnt> alerts!\n";
    }
    else
    {
        print "No alerts found!\n"
    }

    print "Finished processing, elapsed seconds <" . (time() - $startTime) . ">\n";

    exit(0);

I've tried:

    ps -ef|grep myscript.pl|grep $alertCnt grep

which returned:

    52642 117527  0 11:15 pts/63   00:00:00 grep myscript.pl

$alertCnt is the variable which stores the number of alerts that have occurred.I want to count the number of alerts which is given by the variable $alertCnts .What is the right usage of the command to get the number of alert counts here?.

Keep in mind that Perl has a built-in grep function that is quite flexible. You can embed any perl statements you like between curly braces like so:

#!/usr/bin/env perl

grep { 
# do something with regex or whatever.
} `ps -ef` # can be any old command-line invocation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM