Finding filenames

Hi guys
I have the following script that finds all text in files with a search pattern.
Instead of finding the text WITHIN files, Id like to just find the NAMES of the filenames instead.
Any help appreciated.

========================================================= script.pl
use strict;
use warnings;
use Cwd;
use File::Find;

my $search_pattern=$ARGV[0];
my $file_pattern  =$ARGV[1];

find(\&d, cwd);

sub d {

  my $file = $File::Find::name;

  $file =~ s,/,\\,g;

  return unless -f $file;
  return  unless $file =~ /$file_pattern/;

  open F, $file or print "couldn't open $file\n" && return;

  while (<F>) {
    if (my ($found) = m/($search_pattern)/o) {
      print "found $found in $file\n";
      last;
    }
  }

  close F;
}
========================================================== Current usage:

script.pl <searchpattern> txt
eg. script.pl testing txt
will find all txt files that have the word 'testing' in it.
Instead of this, id like to find filenames that have the word testing in it.
LVL 1
Simon336697Asked:
Who is Participating?
 
Kim RyanIT ConsultantCommented:
Only needed a few changes, try this.

use strict;
use warnings;
use Cwd;
use File::Find;

my $search_pattern=$ARGV[0];
my $file_pattern  =$ARGV[1];

find(\&d, cwd);

sub d {

  my $file = $File::Find::name;

  $file =~ s,/,\\,g;

  return unless -f $file;
  return  unless $file =~ /$file_pattern/;

   if ( $file =~ /$search_pattern/) {
      print "found /$search_pattern/) in $file\n";
   }
}
0
 
ozoCommented:
script.pl ^ "testing.*\.txt"
should do that with the existing script
0
 
Simon336697Author Commented:
Thanks guys really appreciate it.
S
0
 
Adam314Commented:
You could also use grep -l
0
 
Simon336697Author Commented:
Thanks adam!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.