Solved

Searching and reading all HTML files

Posted on 2001-06-07
3
161 Views
Last Modified: 2010-03-05
I want to either use a program or write a perl program to put a simple search function on my website.  I want to read all html files in all directories that reside under the main directory.  I want to be able to count the occurences of a user entered string that are in each file.  Does anyone know how to code Perl to:

1.  Loop through all files in all directories and read their contents.
2.  Count the occurances of a given string in a file.

Thanks for your help
0
Comment
Question by:mzehner
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 100 total points
ID: 6165959
Try this program. To run it, you specify the string, optionally followed by the starting directory. For example:

perl search.pl "your string" ./data

use File::Find;

my $search_string = shift(@ARGV);
@ARGV = ('.') unless @ARGV;
my $occurencs;

sub count_string
{
    return unless $File::Find::name =~ /\.html$/;
    open(CURRENT_FH,"<$_");
    @lines = <CURRENT_FH>;
   
    foreach $line (@lines)
    {
        # search for string as a seperate word, case insensitve
         1 while ( $line =~ /\b$search_string\b/ig and ++$occurences);
    }
    if ($occurences)
    {
      print("String $search_string occurs $occurences times in file $File::Find::name\n");
    }
    close(CURRENT_FH);
    $occurences = 0;
}

find(\&count_string, @ARGV);

0
 
LVL 2

Author Comment

by:mzehner
ID: 6168345
It works on a local Linux computer.  I'll try it this weekend on the webserver at tripod.com.  I'll let you know how it goes.

Thanks for your help.
0
 
LVL 2

Author Comment

by:mzehner
ID: 6193640
I have been unable to use the script on Tripod, however I know it works and still hope to use it.  I understand most of the script now.  I'll try to contact Tripod to get the problem solved.

Thanks very much for your help.  It is an excellent and short program.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now