#Replace cwd with `pwd` on *ix
# Script that browses recursively all the dirs down the one you call from
# Find's all the pattern occurencies you need
# Print the name of the file with relative path, lines where is has found
# and the lines themselves
(@ARGV<1) && (die "Must specify a search string\n"); $pattern=$ARGV[0]; $startdir="."; chop ($root=cwd); print "\n\nRoot Dir is $root\n"; $dir=$startdir; &recursive($dir); sub search_dir { local($dir)=@_; opendir (DIR,".") || die ("Can't open "."\n"); @files=readdir DIR; close DIR; chop ($long_dir=cwd); $long_dir =~ s($root)([ROOT]); foreach $file (@files){ unless (-d $file){ open (FILE,$file) || die ("Can't open $file\n"); $count=0; $found=0; while($line=
$count++;
if ($line=~ /$pattern/) {
($found) || (print ("\n$long_dir/$file:\n"));
$found=1;
printf ("[%4d] %s",$count,$line);
}
}
close FILE;
}
}
}
sub recursive {
local($dir)=@_;
#print "Dir=$dir\n";
&search_dir($dir);
foreach $dir (<*>) {
if (-d $dir) {
chdir $dir;
&recursive($dir);
chdir "..";
}
}
}
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!