On Sun, Aug 8, 2010 at 20:40, Jim Green <[email protected]> wrote:
> Hi,
> I used to use find, a for loop and awk to extract data from a list of
> files returned by find.
>
> Now I want to use file::find and perl to this.
>
> use vars qw/*name *dir *prune/;
> *name = *File::Find::name;
> *dir = *File::Find::dir;
> *prune = *File::Find::prune;
>
> my $directories_to_seach="/home/jim";
>
>
> sub wanted;
>
> # Traverse desired filesystems
> File::Find::find({wanted => \&wanted}, $directories_to_seach);
> exit;
>
> sub wanted {
> /regex/s
> && print("$name\n");
> }
>
> my question is how to do in a native, elegant perl way for my bash
> script?
>
> for file in `find . -name "*pattern*"`
> do
> zcat $file|awk '$2 == "BP" {print $17 $18}'|\
> echo
> done
>
>
> Thank you!
>
> Jim
The Perl 5 core way:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
find sub {
return unless -f and /pattern/;
open my $fh, "-|", "zcat", $File::Find::name
or die "could not open $File::Find::name: $!\n";
while (<$fh>) {
#splits on zero or more whitespace by default
my @fields = split;
next unless $fields[1] eq "BP";
print "@fields[16, 17]\n";
}
}, ".";
The CPAN way:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use PerlIO::gzip;
find sub {
return unless -f and /pattern/;
open my $fh, "<:gzip", $File::Find::name
or die "could not open $File::Find::name: $!\n";
while (<$fh>) {
#splits on zero or more whitespace by default
my @fields = split;
next unless $fields[1] eq "BP";
print "@fields[16, 17]\n";
}
}, ".";
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/