Link to home
Start Free TrialLog in
Avatar of sjaguar13
sjaguar13

asked on

Input String, Open Files, Find What File Contains That String.

Basically I want to do what the title says. When someone enters a string, I want the cgi to open text files or html files and see if one of those files contain the string that was entered. Like searching pages. I know how enter the string and pick it up in the cgi, I know how to open a text file, I'm not positive how to do mulitple files, and I'm guess the search part would be If *<string>* = File1 then It's the right file else check if the string is equal to File2. Am I doing this right or is there a better way or do you have any suggestions?
Avatar of rag2000
rag2000

I could post a script that I use, however, you will have to customise it yourself.
Avatar of sjaguar13

ASKER

Does the script you use do all that? I could try to customize it, I don't know if I could do it, but it's worth a try.
hi, try this... it is a rather crude script, but does what you want

#!/usr/bin/perl

     require ("cgi-lib.pl");
      &ReadParse(*Input);

     $words=$Input{'words'};
     $search_words=$Input{'words'};
     @words=split(/\s/,$words);

     $dir='/home/httpd/html/folderOne/';
     chdir($dir);
     $ls=`ls *.html`;
     @ls=split(/\s/,$ls);
     foreach $ls (@ls){ $ds="folderOne/$ls"; }

     $dir='/home/httpd/html/folderTwo/';
     chdir($dir);
     $ftwo=`ls *.html`;
     @ftwo=split(/\s/,$ftwo);
     foreach $ftwo (@ftwo){ $ftwo="folderTwo/$ftwo"; }

     push (@ls, @ftwo);

     foreach $ls (@ls){
          open(FILE,"/home/httpd/html/$ls");
          @lines=<FILE>;
          close(FILE);
          $string=join(' ',@lines);
          $string=~ s/\n//g;

          foreach $words (@words){
               if (!($string=~ /\b$words\b/i)){
                    $include{$ls}='no';
               } else{
                    $include{$ls}='yes';
               }
          }
          if ($string =~ /<title>(.*)<\/title>/i){
               $titles{$ls}="$1";
          } else {
               $titles{$ls}="$ls";
          }
     }

     foreach $key (keys %include){
          if ($include{$key} eq 'yes'){
               $search_results.="<li><a href=\"http:\/\/www.sampatti.com\/$key\">$titles{$key}<\/a><\/li><br><br>";
          }
     }

     $search_results="<p>Your search for '<b>$search_words<\/b>' yielded the following results<\/p><ol>$search_results<\/ol>";

     if($search_results eq "<p>Your search for '<b>$search_words<\/b>' yielded the following results<\/p><ol><\/ol>"){
          $search_results="Sorry, no results matching your keywords '<b>$search_words<\/b>' were found";
     }

     print "Content-type: text/html \n\n";
     open(HANDLE,"/home/httpd/html/sitesearch.html") || print "could not open sitesearch";
     while(<HANDLE>){
          $_=~s/Search Results/$search_results/;
          print $_;
     }
print qx/grep "$string" file1 file2 file2/;
shlomoy:

shorter, does not mean more correct. Even if the previous example is huge and ugly.

===================== simple.pl ===================
#!/usr/bin/perl -w
use strict;
use CGI;
use File::Find;

my $query=new CGI;
print $query->header, $query->start_html;

my $string=$query->param('string');

my @found;
sub search {
    open F,"< $_" or return;
    while(defined(my $line=<F>)) {
        if (index($line, $string)!=-1) {
            push @found, $_;
            close F;
            return;
        }
     }  
    close F;
}

find \&search, '/home/httpd/htdocs';

foreach (@found) {
    print $_, $query->br;
}

print $query->end_html;
==================== EOF ====================

- Sapa
I was under the impression that "grep" is the required functionality.
Dear,

I dont know from where u r picking the list of the files, But i am giving u the example from a directory.

pls try this code hopefully it will help u.

open(DIR,"name of dir here");
@files = readdir(DIR);
foreach $file(@files)
{
open(FILE,"$file");
While(<FILE>)
{
chop;
if(/$string_to_search/)
{
#then wat do u want.
}
}
close(FILE);
}
close(DIR);
Ok, I'm not exactly sure what I'm doing. I'll try to explain what I got from each script:

    $dir='/home/httpd/html/folderOne/'; First directory
    chdir($dir); Not exactly sure what this is, but I      know it does something with the directory

    $ls=`ls *.html`; Lists all html files
    @ls=split(/\s/,$ls); not sure
    foreach $ls (@ls){ $ds="folderOne/$ls"; } no idea, but it involves each html file

    $dir='/home/httpd/html/folderTwo/'; Second directory
    chdir($dir); same as the first
    $ftwo=`ls *.html`; same as the first
    @ftwo=split(/\s/,$ftwo); same as the first
    foreach $ftwo (@ftwo){ $ftwo="folderTwo/$ftwo"; } same as first

    push (@ls, @ftwo); don't know

    foreach $ls (@ls){ don't know
         open(FILE,"/home/httpd/html/$ls"); opens the html files

         @lines=<FILE>; makes an array of the files
         close(FILE); closes the files
         $string=join(' ',@lines); not sure
         $string=~ s/\n//g; not sure

         foreach $words (@words){ for each of the input words

              if (!($string=~ /\b$words\b/i)){ if the words match the file?

                   $include{$ls}='no'; don't match
              } else{
                   $include{$ls}='yes'; match
              }
         }
         if ($string =~ /<title>(.*)<\/title>/i){ don't
              $titles{$ls}="$1"; know
         } else {
              $titles{$ls}="$ls"; about these
         }
    }
NOT SURE ABOUT ANYTHING BELOW

    foreach $key (keys %include){
         if ($include{$key} eq 'yes'){
              $search_results.="<li><a href=\"http:\/\/www.sampatti.com\/$key\">$titles{$key}<\/a><\/li><br><br>";
         }
    }

    $search_results="<p>Your search for '<b>$search_words<\/b>' yielded the following results<\/p><ol>$search_results<\/ol>";

    if($search_results eq "<p>Your search for '<b>$search_words<\/b>' yielded the following results<\/p><ol><\/ol>"){
         $search_results="Sorry, no results matching your keywords '<b>$search_words<\/b>' were found";
    }

    print "Content-type: text/html \n\n";
    open(HANDLE,"/home/httpd/html/sitesearch.html") || print "could not open sitesearch";
    while(<HANDLE>){
         $_=~s/Search Results/$search_results/;
         print $_;
    }

_________________________________________________________


print qx/grep "$string" file1 file2 file2/;

this seems the easiest but is confusing. What is it supposed to print?

_________________________________________________________


Sapa's is confusing too. I don't understand all the variables and what is being prinited if the string is found or not.

_________________________________________________________


open(DIR,"name of dir here"); Opens whatever directory
@files = readdir(DIR); Creates an array
foreach $file(@files) for each file
{
open(FILE,"$file"); opens each one
While(<FILE>) don't get this
{
chop; chops something
if(/$string_to_search/) if the string is found?
{
#then wat do u want. Does what, shows the match
}
}
close(FILE); not found, it closes the file
}
close(DIR); closes the directory

_________________________________________________________


I know I don't know a lot about perl. I would like to learn more, but I'm learning stuff slowly. If I got any of this wrong and/or you can explain the uncertain parts, please tell me.
#!C:\perl\bin\perl.exe

require "cgi-lib.pl";
&ReadParse;

$string_to_search=$in{'search'};

open(DIR,"Pages");
@files = readdir(DIR);
foreach $file(@files)
{
open(FILE,"$file");
While(<FILE>)
{
chop;
if($string_to_search)
{
#then wat do u want.
print "Content-type: text/html\n\n";
print "<HTML><HEAD>\n";
print "<TITLE>CGI Test</TITLE>\n";
print "</HEAD>\n";
print "<BODY><textarea rows=25 cols=55>HI</textarea>.\n";
print "</BODY></HTML>";
}
}
close(FILE);
}
close(DIR);


That's what I have and I get a premature of script headers error. What am I doing wrong? That crap it's printing is just there to see if it works, it won't be in the final version. This is just a test script with three files, 1.txt, 2.txt, and 3.txt. Let me know what's wrong, PLEASE!!!
When I use Rag2000's script, I get "could not open sitesearch"
sjaguar13 --->  You've asked a total of 24 questions and only closed/graded 10 of them.  Some are more than ONE YEAR old, this is unacceptable.  Please click the HELP DESK link on the left which contains our Guidelines and Agreement as well as information on site-related help files on questions and answers.

I will update all your open questions with this information so that you will be advised by Email notification and can navigate through them to finalize them.

Your responsiveness is needed on all of them.  I will monitor for closure.  If you need help in terms of splitting points between participants or other related assistance, please comment with details and I will return as quickly as I can.

Thank you,
Moondancer
Community Support Moderator @ Experts Exchange
Please accept the comment which helped you to then grade and close this.  If more is needed, let the experts know.  If you wish to award more than one, just comment with details and I'll handle for you.

Thank you,
Moondancer
Community Support Moderator @ Experts Exchange
ASKER CERTIFIED SOLUTION
Avatar of Moondancer
Moondancer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Moved to PAQ at zero points and closed.
Moondancer
Community Support Moderator @ Experts Exchange