Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

counting occurance of a particular phrase in a file



7:01 am on Jul 29, 2004 (gmt 0)

10+ Year Member

Here is my code
for counting occurence of a phrase in a file...
the prob here which i am facing is that when 1st time
the phrase occurs it increments the counter and comes out of the loop and shows we count = 1..
it does go upto end of the loop.

plz help

open(FILE, "title.txt");
while (<FILE>) {

if(eof()) { # Not eof().

while($_ =~ m/[\w\W\s]*$in[\w\W\s]*/ig){

print OUT "Total no of times $arr1[$j] occured is : $k \n" ;

print "$arr1[$j] : $k \n" ;


9:10 am on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member kaled is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Your use of eof appears unnecessary (and wrong) - I think you're only scanning the last line of code (when eof is true).

As a side-bar, I believe that if you substitute the search string with a null string, the returned value is the number of substitutions made.

e.g. $x = ($_ =~ s/$search//ig);


I've not used Perl much, so don't take anything above as definitive.


3:46 pm on Aug 16, 2004 (gmt 0)

10+ Year Member

Make sure that you are in 'use strict' mode.

Declare my $k outside of the loop to ensure that it is not local to the loop as it will be overwritten each time the loop runs as opposed to being incremented.

You should be able to just....

use strict;
my $k;
if($_ =~ m/[\w\W\s]*$in[\w\W\s]*/ig)
print $k;

run from command line as:
perl myscript.pl text.txt


4:58 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

A regular expression may not be the most efficient solution here. This would probably run faster:

open (FILE, "file.txt");
$/ = $in;

my $count;
while (<FILE>) {

print $count;


7:22 pm on Aug 16, 2004 (gmt 0)

10+ Year Member

G'day. If you want to know how many times 'blahblah' appears in test.txt, do this:

perl -ne 'END { print "Word appeared $t times\n" } @w = /(blahblah)/g; $t += @w' test.txt

If you're still confuzled, check this out:

And if you just want to know how many lines a phrase appears in, do this:

grep blahblah test.txt wc -l
(Assuming you're using a real operating system like linux of course)

And if you have a whole directory structure with tons of html files and you want to spell your misspelling of mispelled correctly, you can do this:

perl -pi -e 's/mispelled/misspelled/gi' `find . -name "*.html"`

Rock and Roll.


Featured Threads

Hot Threads This Week

Hot Threads This Month