Solved

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

Posted on 2001-06-19
14
304 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl - Mawk 2 69
compress files in RAR using perl 13 73
crawling gofundme 4 103
Perl troubleshooting error. Base64 bootstrap parameter 5 60
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 …
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

20 Experts available now in Live!

Get 1:1 Help Now