Here's another way, but not necessarily the best Perl, but it does work:
#!/usr/bin/env perl
use strict;
use warnings;
my @filelist=<STDIN>;
chomp @filelist;
my $match_strings=qr '(abc123blue|xyz357green)';
my ($file, );
foreach $file (@filelist) {
#print "file=$file\n";
open(FD,"<$file") or die "Cannot open $file\n";
my $iline;
while ($iline=<FD>) {
chomp $iline;
print "$file\n" if ($iline =~ /$match_strings/);
}
close FD;
}
It has the benefit of taking a pretty large list of files on stdin and lets
you visually that the files its selecting are the ones you want on stdout.
ls -1 files | perlprog| xargs rm -v --
It does still suffer from handling filenames containging nonprinting
characters. If you are happy that the code is OK, substitite the print
statement with unlink.
--
Andrew in Edinburgh,Scotland
On Sat, 6 Sep 2008, brian54321uk wrote:
Peter Scott wrote:
On Fri, 05 Sep 2008 19:09:25 +0100, brian54321uk wrote:
I would like to test a folder full of files, and if a file contains
abc123blue or xyz357green then that file is to be deleted.
What would be the best way of achieving this please?
One way:
use strict;
use warnings;
sub delete_containing {
my ($folder, @strings) = @_;
my $regex = join '|', map { quotemeta } @strings;
unlink grep { contains( qr/$regex/, $_ ) } glob "$folder/*";
}
sub contains {
my $regex = shift;
local @ARGV = @_;
local $_;
/$regex/ and return 1 while <>;
return;
}
delete_containing( '/tmp/bar', 'abc', 'xyz' );
Just tried this, nothing happened at all.
Brian
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/