Recursive Search and Grep Utility Written by sierrathedog04, sierrathedog04@hotmail.com, 6/01
- The first parameter is a regular expression which matches the filenames that you wish to search
for. It defaults to '.*'
- The second parameter is a regular expression for which you wish to grep your found files. It
defaults to '.*'
- The third parameter is the directory from which you will start your recursive search. It
defaults to '.'
Tested on Perl 5.005_02 in a MP-RAS Unix environment. Updated 6/19 to add line inadvertently left off original posting.
use strict;
use File::Find;
my $glob = shift || '.*';
my $grepTerm = shift || '.*';
my $dir = shift || ".";
#following line added 6/19. (Inadvertently left off original posting:)
find (\&Wanted, $dir);
sub Wanted {
return unless /$glob/;
open (CURRENT_FILE, $_);
my @currentFile = <CURRENT_FILE>;
my @foundLines = grep /$grepTerm/, @currentFile;
print "In $File::Find::dir/$_:\n" if @foundLines;
foreach (@foundLines) {
print " $_\n";
}
}
print "Completed recursive search for files named $glob containing ter
+m $grepTerm in directory $dir";
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|