Forum Moderators: coopster & phranque

Message Too Old, No Replies

Searching a text file for how many times something appears in it

Newbie...

         

shell

11:47 am on May 23, 2003 (gmt 0)

10+ Year Member



I'm a newbie first off, so if any one could point me at some good websites also, that would be handy :) Is there anything out there like the PHP manual but for Perl?

On with the Q...
I have a text file I wish to search to find out how many times '¦needauth' appears in it. Warning: There is no space before the search phrase!

How? :/

shell

12:45 pm on May 23, 2003 (gmt 0)

10+ Year Member



Doh! Just to make it clear, I want to know how to do this in Perl... sorry.

BCMG_Scott

12:54 pm on May 23, 2003 (gmt 0)

10+ Year Member



perldoc.com man pages are close to the PHP manual, although not as "user friendly" per se.

you can use the grep command to get this. You'd need to read the file into an array, then grep it for "¦needauth", then get the count of the new array.

So assume you read your file into @file:

@parse = grep (/\¦needauth/,@file);
$num_times = $#parse + 1;

$#parse is the last element number of the array @parse. Since arrays begin with 0 you need to add 1 to get a "human" count.

Scott Geiger