search dirs

im a total novice to perl

id like code to do this
i want a drop down menu and a text
box and when people pick one option
from the drop down menu and enter a word
they will be given results.

the options in the drop down menu are
"X" "Y" and "Z" and these correspond to different directories (X Y and Z)

if i enter Z and the word "and"
i will be given these results

-the name of the file that matches the search
-the exact line that matches the search with the searched for word in bold
-a link that can be clicked which lets the use read the whole file (id like the text to come up in my browser in the same format as it is in the text file)

thank you
boofullsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
guadalupeConnect With a Mentor Commented:
OK this is a long one so well go in parts:

The main script is here once we get this going the only thing left is to show the text and that is a snap... So here we go:

#!/usr/local/bin/perl

#Turn on auto flush to avoid browser time out waiting for long dir seraches
{

}
$| = 1;

use File::Find;

&parseInput;

#assume the combo box is named dir and the value sent is the complete dir
#ie C:/Text_Docs or /home/usr/text_docs

#assume that the text box for the search word is named word
#set to scalar for use in regex
$word = $fields{'word'};


#print header to browser
print <<EOF;
Content-type: text/html\n\n
<HTML>
<HEAD>
<TITLE> XXXXXXXXXXXXXX </TITLE>
<HEAD>
<BODY>
Word found in the following docs:
<PRE>
EOF

# This will call sub rename_it for each file found, and will recurse
find(\&find_word, "$fields{'dir'});

#End html doc
print <<EOF;
</PRE>
</BODY>
</HTML>
EOF


sub find_word
{
      # $File::Find::name now has the full path and name of the file
      open(FILE, "$File::Find::name")

            while ($line = <FILE>)
            {
                  #match word use i for case INSENITIVE
                  if ($line =~ /$word/i)
                  {
                        #Make bold and add link
                        $line =~ s/($word)/<a href="show_text.cgi?$File::Find::name"><b>$1</b></a>/g;
                        
                        #Output line to browser
                        print "\n$line\n";
                  }
            }
}

sub parseInput()
{
      #Parse form variables
      if ($ENV{'REQUEST_METHOD'} eq "POST")
      {
            read(STDIN, $temp, $ENV{'CONTENT_LENGTH'});
      }

      elsif ($ENV{'REQUEST_METHOD'} eq "GET" )
      {
            $temp = $ENV{'QUERY_STRING'};
      }

      @pairs=split(/&/,$temp);
      
      foreach $item(@pairs)
      {
            ($key,$content)=split (/=/,$item,2);
            $content=~tr/+/ /;
            $content=~ s/%(..)/pack("c",hex($1))/ge;
            $fields{$key}=$content;
      }

}
0
 
boofullsAuthor Commented:
also can i have an optino "part of word" or "exact word" ( a radio button)

if they select part of word then when they enter "and" they will get entries like "sand" and if they select exact word they will just get that exact word

0
 
boofullsAuthor Commented:
ok im really conused with your answer but thanks for helping
can u post a basic working version that i can work on and improve?
and tell me what to improve in comments?

thanks
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
guadalupeCommented:
That really is kinda basic in relation to what you want.  But tell me what part you need help with and I'll try and clarify...
0
 
boofullsAuthor Commented:
can u post a simple html interface to a perl script that searches....

i cant seem to work out how to put it all together

thanks
0
 
guadalupeCommented:
Are you on Unix or NT?  Its a small difference but lets do it right...
0
 
boofullsAuthor Commented:
unix
0
 
guadalupeCommented:
OK, the html doc which has the form and calls th cgi would be like this:
###############################

<HTML>
<HEAD>
<TITLE> New Document </TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">


<FORM METHOD="POST" ACTION="dir_search.cgi">
Choose Dir: <SELECT NAME="dir">

<!--      CAUTION THE DIRS I HAVE PLACED AS VALUES IN THE OPTIONS MUST BE CHANGED
            TO REAL DIRS ON YOUR SERVER!!!!  DIRS WHICH YOU HAVE ACCESS TO!!!!!!!!! -->


<option value="/usr/home/docs">Text Dir</option>
<option value="/usr/home/htdocs">HTML Dir</option>

</SELECT><br>

Search Word: <INPUT TYPE="text" NAME="word"><br>

<INPUT TYPE="submit">
</FORM>



</BODY>
</HTML>

#####################################
Read comment in above file!!!!

Then tage the cgi script I submitted above and save it as dir_search.cgi and remember to set the permissions to 775 using "chmod 775 dir_serach.cgi"  or the same comand mangaed by your FTP prog such as WS-FTP.  This should do it...
0
 
guadalupeCommented:
Ah one more thing.  The following code:

#!/usr/local/bin/perl

#Turn on auto flush to avoid browser time out waiting for long dir seraches
{

}
$| = 1;

use File::Find;

&parseInput;

Change to:

#!/usr/local/bin/perl

#Turn on auto flush to avoid browser time out waiting for long dir seraches
 
$| = 1;

use File::Find;

&parseInput;

Merely removing a pair of brackets which were useless.
0
 
guadalupeCommented:
# This will call sub rename_it for each file found, and will recurse
find(\&find_word, "$fields{'dir'}");

Sorry don't know how I did that.
0
 
guadalupeCommented:
sub find_word
{
# $File::Find::name now has the full path and name of the file
open(FILE, "$File::Find::name");

while ($line = <FILE>)
{
#match word use i for case INSENITIVE
if ($line =~ /$word/i)
{
#Make bold and add link
$line =~ s!($word)!<a href="show_text\.cgi\?$File::Find::name"><b>$1</b></a>!g;

#Output line to browser
print "\n$line\n";
}
}
}

0
 
boofullsAuthor Commented:
beside each match that is returned can u print the name of the file in which the match appears?
thanks
0
 
guadalupeCommented:
#Make bold and add link
$line =~ s!($word)!<a href="show_text\.cgi\?$File::Find::name"><b>File: $File::Find::name - $1</b></a>!g;
0
 
guadalupeCommented:
Thank you for the double points and fogive me the mistakes, I've been busy and trying to do too many things at once.  Thanks for the patience.
0
 
boofullsAuthor Commented:
no problem, thanks for your help, i would have been lost without u....
0
All Courses

From novice to tech pro — start learning today.