Try the following:
use File::Basename; { local $/; opendir(DIR, $dirname) or die "Can't open directory $dirname: $!"; while (defined($file = readdir(DIR))) { ($base, $dir, $ext) = fileparse($dirname . "/" . $file,'\..*'); if ($file ne "." && $file ne "..") { #-T) { open(FILE, $dirname."/".$file) or die "Couldn't open $file: $!"; $plain_text = <FILE>; $plain_text =~ s/<[^>]*>//gs; if ($plain_text =~ /$search_string/g) { push(@matched_files, $base); } close(FILE); } } closedir(DIR); } # @matched_files contains all files that had $search_string in them
Set $dirname to the path of the directory you want to search in and $search_string to the string you want to search for. Note: There is a problem with this script in that it will not strip html properly if there are nested tags for example the tag:
<img src="img.gif" alt=" Look at this >>>> ">
wouldn't be stripped properly. If this isn't going to be a problem though then the solution above should work fine. Good luck! Rob

In reply to Re: Searching text files with strings by r3b3lxd
in thread Searching text files with strings by JohnMcPherson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.