Perl function similar to wc in grep -c?
I am trying to write a script that goes through an entire folder of text files, matching a string pattern. What I want is the count of patterns matched i开发者_如何学Cn each file. In unix it could be done by grep -c <pattern> *
. where the -c
option returns you the count. Is there any way to get that count using perl regex?
Please let me know.
my $grep_count = grep (/PATTERN/, @array);
Or for a file:
perl -we "print scalar grep /PATTERN/, <>;" file.txt
http://perldoc.perl.org/functions/grep.html
Not that I know of, but just write it, when you open each file, use while
to go through each of its lines, and use =~ against each line (with your pattern), if matched, then add 1 to your counter.
精彩评论