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: Beginner:
Search for string and print to output file

 



effc
Novice

Dec 6, 2012, 5:09 AM

Post #1 of 2 (982 views)
Search for string and print to output file Can't Post

I am attempting to write a perl script to search every file in a directory for a string. Unfortunately when I run it my output file is empty, but I know there are some in there! Help



use strict;
use warnings;

my $string = 'Reproductive exam'; #string that I am looking for
my $file = $_;


open my $results, '>', 'results.txt' or die "Unable to open results file: $!"; #this works


sub printFile {

return unless -f and /\.sift$/;

open my $fh, '<', , $_ or do {
warn qq(Unable to open "$File::Find::name" for reading: $!);
return;
};

if (/\Q$string/) {
print $results "$File::Find::name\n";
return;
}
}


FishMonger
Veteran / Moderator

Dec 6, 2012, 7:38 AM

Post #2 of 2 (977 views)
Re: [effc] Search for string and print to output file [In reply to] Can't Post


Quote
my $file = $_;

That line is not doing what you think.

If you want to pass the filename as a command line arg, then that line should be one of these:

Code
my $file = shift; 
my $file = shift @ARGV;
my $file = $ARGV[0];


You never called the printFile() sub, so there's no reason to expect results.txt to have any data.

 
 


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

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