#!/usr/bin/env perl
use strict;
use warnings;
my $pat = shift;
sub interleave($$){
my ($foo,$bar) = @_;
return map { ( $_ , shift @{$bar} ) } @{$foo};
}
my $n=0;
my(@p,%h);
while(<>){
chomp;
if($n%3==0){
@p = split /\t/, $_;
} elsif($n%3==2){
my @l = split /\t/, $_;
my %kv = interleave(\@p, \@l);
foreach my $k (keys %kv){
push(@{$h{$kv{$k}}}, $k);
}
}
$n++;
}
foreach my $loc (keys %h){
if(!defined $pat || $loc =~ /$pat/i){
foreach my $name (@{$h{$loc}}){
print ucfirst($loc), "\t", ucfirst($name), "\n";
}
}
}
А потом назовите это
perl extract.pl 'texas|florida' < data
Форма "Oneliner":
perl -ne 'BEGIN{$p=shift||"^";}chomp;if($n++%3!=1){unless(@p){@p=split/\t/,$_;next;}my %kv = map { ( $_ , shift @p ) } split(/\t/, $_);map { push(@{$h{$_}}, $kv{$_}); } keys %kv;}END{map{for my$nm(@{$h{$_}}){print ucfirst($_),"\t",ucfirst($nm),"\n";}}grep{/$p/i}keys%h;}' 'florida|texas' < data