?
Solved

Finding filenames

Posted on 2007-08-01
5
Medium Priority
?
172 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:Simon336697
5 Comments
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 1880 total points
ID: 19613531
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
 
LVL 85

Assisted Solution

by:ozo
ozo earned 120 total points
ID: 19613852
script.pl ^ "testing.*\.txt"
should do that with the existing script
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19614250
Thanks guys really appreciate it.
S
0
 
LVL 39

Expert Comment

by:Adam314
ID: 19617277
You could also use grep -l
0
 
LVL 1

Author Comment

by:Simon336697
ID: 19620351
Thanks adam!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question