Solved

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

Posted on 2001-06-19
14
322 Views
Last Modified: 2010-07-27
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?
0
Comment
Question by:sjaguar13
  • 4
  • 4
  • 2
  • +3
14 Comments
 
LVL 3

Expert Comment

by:rag2000
ID: 6209219
I could post a script that I use, however, you will have to customise it yourself.
0
 

Author Comment

by:sjaguar13
ID: 6209226
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.
0
 
LVL 3

Expert Comment

by:rag2000
ID: 6209288
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 $_;
     }
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 8

Expert Comment

by:shlomoy
ID: 6209453
print qx/grep "$string" file1 file2 file2/;
0
 
LVL 5

Expert Comment

by:Sapa
ID: 6209984
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
0
 
LVL 8

Expert Comment

by:shlomoy
ID: 6213047
I was under the impression that "grep" is the required functionality.
0
 
LVL 2

Expert Comment

by:shirjeel
ID: 6223617
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);
0
 

Author Comment

by:sjaguar13
ID: 6226446
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.
0
 

Author Comment

by:sjaguar13
ID: 6263182
#!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!!!
0
 

Author Comment

by:sjaguar13
ID: 6263281
When I use Rag2000's script, I get "could not open sitesearch"
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6702833
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
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6704200
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
0
 
LVL 1

Accepted Solution

by:
Moondancer earned 0 total points
ID: 6758553
Your points were refunded to you for this question.  You may now wish to ask the question new to draw current attention.  HOWEVER, it is important that you remain active in all your open questions here, and provide feedback if more is needed (as you did here), and award points to the expert(s) who helped you when the solution has been found.  I've sent many follow up requests in all your open questions, some of which dated back to the year 2000, but were never closed.  We had to close them for you.

Please check the HELP DESK link on the left for details on this site in terms of Guidelines, Questions/Answers and additional site-related help.

Your cooperation is not only appreciated, but necessary.

Thank you,

Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6758554
Moved to PAQ at zero points and closed.
Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

803 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