CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Searching a test file

 



kmcleod
Deleted

Oct 24, 2000, 9:43 AM

Post #1 of 2 (240 views)
Searching a test file Can't Post

Can I use perl to search the contents of a test file. The perl search scripts I've seen will search a directory for file names. I need to search the text file contents for keywords. Is this possible?


GERRIT
Deleted

Nov 3, 2000, 12:58 PM

Post #2 of 2 (240 views)
Re: Searching a test file [In reply to] Can't Post

Yes, it's possible, but may take some time to run...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


my $found;
my $entry;
my @file;
my $dirname = "/foo/baz";
opendir(DIR, $dirname) or die "Cannot open dir: $!";
foreach $entry (readdir(DIR)) {

# skip dirs
next if $entry =~ /^\.\.?$/;
next if (-d $entry);

open(FILE, "$dirname/$entry") or die;
@file = <FILE>;
close(FILE);

# search file
for (@file) {

# YEaH!
if($_ =~ /PATTERN_TO_MATCH/) {

$found .= "found in: $dirname/$entry";

}
}
closedir(DIR);

</pre><HR></BLOCKQUOTE>

This might help you to find the right path :)


------------------
hejdå,
GERRIT

(¯`·.¸¸.·´¯`·... gerrit wigger - gerrit@wigger.de

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives