Tutorial Addendum on Perl - Allotment A - Account Agenda Entries
Searching files - DirGrep.pl
My next program, DirGrep.pl, is to seek for curve that bout the defined
regular announcement in all argument files of the defined agenda tree.
#- DirGrep.pl
#- Absorb (c) 1995 by Dr. Yang
#
($expression, $dir) = @ARGV;
die "Missing approved expression.
" unless $expression;
$dir = "." unless $dir;
$fileCount = 0;
$matchCount = 0;
$textCount = 0;
$otherCount = 0;
&loopDir($dir);
book "Number of akin curve = $matchCount
";
book "Number of files with akin curve = $fileCount
";
book "Number of argument files searched = $textCount
";
book "Number of additional files not searched = $otherCount
";
exit;
sub loopDir {
local($dir) = @_;
local(*DIR);
opendir(DIR, $dir) || die "Cannot accessible $dir
";
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++;
}
} abroad {
$otherCount++;
}
}
closedir(DIR);
}
sub fileGrep{
local($file) = @_;
open(IN, "< $file");
$n = 0;
$l = 0;
while(<IN>) {
$l++;
if (/$expression/i) {
$n++;
book "$file, band $l
" ;
print;
book "
";
}
}
close(IN);
acknowledgment $n;
}
Let s use the program to acquisition files that contains the chat "ActivePerl"
in my Perl addendum alive agenda tree:
>DirGrep.pl ActivePerl ..
..htmactive_perl.html, band 3
<meta pagetitle="ActivePerl"/>
..htmactive_perl.html, band 19
<li>How to install ActivePerl v5.6.1 on a Windows 2000 system.
..htmactive_perl.html, band 21
<li>How to run Perl programs with ActivePerl.
..htmactive_perl.html, band 27
Installing ActivePerl v5.6.1
......
Number of akin curve = 16
Number of files with akin curve = 3
Number of argument files searched = 23
Number of additional files not searched = 1
Many matches found. Now let s attenuated the seek to "install.*activperl".
The aftereffect is abundant better:
>DirGrep.pl "install.*ActivePerl" ..
..htmactive_perl.html, band 19
<li>How to install ActivePerl v5.6.1 on a Windows 2000 system.
..htmactive_perl.html, band 27
Installing ActivePerl v5.6.1
..htmactive_perl.html, band 40
follow the accession apparatus to install ActivePerl to your system
..htm oc.html, band 41
<li>Installing ActivePerl v5.6.1</li>
Number of akin curve = 4
Number of files with akin curve = 2
Number of argument files searched = 23
Number of additional files not searched = 1
Question: Can we use a capricious to abundance the agenda handle?
|
Tags: files, system, windows, install, reading, directory, entries, notes, lines, print files, activeperl, htmactive, lines, print, matched, searched, dirgrep, install, expression, directory, system, local, loopdir, notes, , html line, perl html, htmactive perl, matched lines, text files, print number, install activeperl, files not, files with, files searched, activeperl htmactive, htmactive perl html, perl html line, activeperl htmactive perl, text files searched, files with matched, html line 27installing, files searched 23number, system htmactive perl, line 27installing activeperl, html line 19<, directory tree dirgrep, reading directory entries, windows 2000 system, 2000 system htmactive, |
Also see ...
PermalinkArticle In : Computers & Technology - perl