Re: matching elements from array and print the results line by line from log file



slow_leaner wrote:
Hi,

Hello,

I have a list of element in array that I would like to match the
pattern in logs file. I have hard time take element from array and
matching it. More then weeks now and don't know where to find in man
page.

perldoc -q "How do I efficiently match many regular expressions at once"

please help me. Here is my code.

#!/usr/bin/perl -w
open ( FILE, " /var/log/cisco.log " ) || die "can't open cisco.log
file!";
@cisco.log = <FILE>;
close (FILE);

@array = qw( BPDUGUARD FAN_FAIL ); # more then 2 elements in array

while (<@cisco.log>){

That is *not* how you read lines from a file. Remove:

@cisco.log = <FILE>;
close (FILE);

from above and then your while loop should be:

while ( <FILE> ) {

foreach $line (@array){
$_ =~ $line; #DOESN'T WORK AND I AM STUCK..

You are performing the match in void context so it is superfluous. See the FAQ entry above for how to do it.

print "$_\n"; #WANT TO PRINT THAT MATCH
}
}


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages

  • matching elements from array and print the results line by line from log file
    ... I have a list of element in array that I would like to match the ... pattern in logs file. ... I have hard time take element from array and ...
    (perl.beginners)
  • Re: Binary Search
    ... that calls the binary search for finding any matching entry. ... Then that second function looks down the array, as long as it finds matching entries. ... OTOH n is not the range of the whole array, but only the amount of matching elements. ...
    (comp.lang.pascal.delphi.misc)
  • Re: indices into an array
    ... My real array is much larger than the example above ... var filteredArray = a.filter( ... At least the for-loop is probably executed in already compiled code then, ... want the matching values, or the matching indexes? ...
    (comp.lang.javascript)
  • Re: Seach and replace tools
    ... matching an array of strings to be found and ... replaing them. ...
    (uk.comp.sys.mac)
  • Re: Seach and replace tools
    ... matching an array of strings to be found and ... replaing them. ...
    (uk.comp.sys.mac)