DirGrep.pl - Searching Text in Directory Files
DirGrep.pl - Searching Text in Directory Files
This section provides a tutorial example, DirGrep.pl, to search text in files and sub directories using a recursive method to walk through a directory tree.
Perl, Open, Read, Directory
DirGrep.pl - Searching Text in Directory Files
This section provides a tutorial example, DirGrep.pl, to search text in files and sub directories using a recursive method to walk through a directory tree.
My next program, DirGrep.pl, is to search for lines that match the specified regular expression in all text files of the specified directory tree.
#- DirGrep.pl
#- Copyright (c) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#
($expression, $dir) = @ARGV;
die "Missing regular expression.\n" unless $expression;
$dir = "." unless $dir;
$fileCount = 0;
$matchCount = 0;
$textCount = 0;
$otherCount = 0;
&loopDir($dir);
print "Number of matched lines = $matchCount\n";
print "Number of files with matched lines = $fileCount\n";
print "Number of text files searched = $textCount\n";
print "Number of other files not searched = $otherCount\n";
exit;
sub loopDir {
local($dir) = @_;
local(*DIR);
opendir(DIR, $dir) || die "Cannot open $dir\n";
while ($f=readdir(DIR)) {
next if ($f eq "." || $f eq "..");
$f = "$dir\\$f";
if (-d $f) {
&loopDir($f);
} elsif (-T $f) {
$textCount++;
if ($n=&fileGrep($f)) {
$matchCount += $n;
$fileCount++;
}
} else {
$otherCount++;
}
}
closedir(DIR);
}
sub fileGrep{
local($file) = @_;
open(IN, "< $file");
$n = 0;
$l = 0;
while(<IN>) {
$l++;
if (/$expression/i) {
$n++;
print "$file, line $l\n" ;
print;
print "\n";
}
}
close(IN);
return $n;
}
Let's use the program to find files that contains the word "ActivePerl" in my Perl notes working directory tree:
>DirGrep.pl ActivePerl .. ..\htm\active_perl.html, line 3 <meta pagetitle="ActivePerl"/> ..\htm\active_perl.html, line 19 <li>How to install ActivePerl v5.6.1 on a Windows 2000 system. ..\htm\active_perl.html, line 21 <li>How to run Perl programs with ActivePerl. ..\htm\active_perl.html, line 27 Installing ActivePerl v5.6.1 ...... Number of matched lines = 16 Number of files with matched lines = 3 Number of text files searched = 23 Number of other files not searched = 1
Many matches found. Now let's narrow the search to "install.*activperl". The result is much better:
>DirGrep.pl "install.*ActivePerl" .. ..\htm\active_perl.html, line 19 <li>How to install ActivePerl v5.6.1 on a Windows 2000 system. ..\htm\active_perl.html, line 27 Installing ActivePerl v5.6.1 ..\htm\active_perl.html, line 40 follow the installation tool to install ActivePerl to your system ..\htm\toc.html, line 41 <li>Installing ActivePerl v5.6.1</li> Number of matched lines = 4 Number of files with matched lines = 2 Number of text files searched = 23 Number of other files not searched = 1
Question: Can we use a variable to store the directory handle?