After allot of searches like 'find . -type f -exec grep main {} -H \;' I implemented a small program
.
This program uses the normal 'file' shell command to check if a file is a text file.
Ignores files inside /CVS/. They are there, but who cares?
Clearly separates the results from different files
and can show how to use:
the module File::Find
creating subroutine locally
the grep, the last, the -f, ...
get the output from shell commands
...
A 50 lines program.
.
This program uses the normal 'file' shell command to check if a file is a text file.
Ignores files inside /CVS/. They are there, but who cares?
Clearly separates the results from different filesand can show how to use:
the module File::Find
creating subroutine locally
the grep, the last, the -f, ...
get the output from shell commands...
Code: Perl
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
if (grep {/^-help$/} @ARGV) {
print "$0 [-all|-file|-help] matchRegExp [path]";
exit;
}
my $all = grep {/^-all$/} @ARGV;
my $justFile = grep {/^-file$/} @ARGV;
my ($match, $initPath) = @ARGV = grep {not /^-(all|file|help)$/} @ARGV;
die "just one file accepted" if scalar(@ARGV) > 2;
die "no match" if scalar(@ARGV) == 0;
$initPath = (".") if not defined $initPath;
die("unrecognizable path: $initPath") if not -e $initPath;
my $lastPrinted;
find( sub {
my $fileName = $_;
my $filePath = $File::Find::name;
return if $filePath =~ m|/CVS/|; # not inside CVS directory
return if not -f $fileName; # just check files
return if not $all and `file -b $fileName` !~ /text/i; # just text files
open(my $FILE, $fileName) or die "canot open file '$fileName': $!";
while (my $line = <$FILE>) {
if ($line =~ /$match/) {
if ($justFile) {
print("$filePath\n");
last;
} else {
print("\n", "*"x80, "\n\n")
if $lastPrinted and $lastPrinted ne $filePath;
$lastPrinted = $filePath;
printf("%-50s %s", "$filePath $.:", $line);
}
}
}
close($FILE);
}, $initPath);
A 50 lines program.
