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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Search for string and print to output file



Dec 6, 2012, 5:09 AM

Post #1 of 2 (2409 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: $!);

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

Veteran / Moderator

Dec 6, 2012, 7:38 AM

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

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:

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