从表中提取特定数据

时间:2011-12-08 10:48:46

标签: python perl bash shell

我的表格看起来像这样(标签分隔):

Ron  Rob  rock bammy
m    f   m  f
florida  Atlanta  florida texas 

此表的顺序为5 * 512,基于第3行数据,我想提取row1中的值。  例如:我希望所有居住在佛罗里达州和德克萨斯州的人的姓名都在2列和n行的表格中。

Florida  Ron
Florida  Rock
Texas BAmmy

等等。

对bash或PERL内衬的任何建议......

提前谢谢。

5 个答案:

答案 0 :(得分:1)

awk 'NR==1{for(i=1;i<=NF;i++)n[i]=$i}; NR==3{for(i=1;i<=NF;i++){if($i~/florida|texas/)print $i"\t"n[i];}}' yourFile

见下面的测试:

kent$  echo "Ron Rob rock bammy
m f m f
florida Atlanta florida texas"|awk 'NR==1{for(i=1;i<=NF;i++)n[i]=$i}; NR==3{for(i=1;i<=NF;i++){if($i~/florida|texas/)print $i"\t"n[i];}}'

<强>输出

florida Ron
florida rock
texas   bammy

修改

kent$  echo "Ron  Rob  rock bammy
m    f   m  f
florida(8)  Atlanta  florida(8) texas(2;7)"|awk 'NR==1{for(i=1;i<=NF;i++)n[i]=$i}; NR==3{for(i=1;i<=NF;i++){if($i~/florida\(8\)|texas\(2;7\)/)print $i"\t"n[i];}}'

输出:

florida(8)      Ron
florida(8)      rock
texas(2;7)      bammy

答案 1 :(得分:1)

又一个Perl解决方案:

perl -ane 'push@c,@F}{print grep{/^(florida|atlanta)\t/i}map{"$c[$_+$#c/3*2+1]\t$c[$_]\n"}0..$#c/3'

或作为剧本

#!/usr/bin/perl

use strict;
use warnings;

my (@data, @rows);

push @data, split/\s+/ while (<>);

for (0 .. $#data/3) {
    my $name = $data[$_];
    my $location = $data[$_+$#data/3*2+1];
    push @rows, "$location\t$name\n" if $location =~ /^(florida|atlanta)$/i;
}

print join("", @rows);

在循环内部使用if条件而不是单独的grep

我的方法是将所有三行展平为一个数组,并使用for (0 .. $#data/3)循环对应于第一行中名称的索引,并使用$data[$_+$#data/3*2+1]从匹配列中获取位置。

答案 2 :(得分:0)

这是一个可行的Perl解决方案,但我更喜欢这个解决方案。将这些数据放入数据库可能会更好。

#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

my %rows = (
  name => 1,
  location => 3,
);

my %location = map { $_ => 1 } qw[florida texas];

my @names;

while (<DATA>) {
  next unless grep { $_ == $. } values %rows;

  chomp;

  if ($. == $rows{name}) {
    @names = split;
  }

  if ($. == $rows{location}) {
    my @locs = split;

    for my $x (0 .. $#locs) {
      if ($location{lc $locs[$x]}) {
        say ucfirst $locs[$x]. "\t$names[$x]";
      }
    }
    last;
  }
}

__END__
Ron     Rob     rock    bammy
m       f       m       f
florida         Atlanta florida texas

答案 3 :(得分:0)

#!/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

答案 4 :(得分:0)

听起来像这是Text::CSV_XS的工作。正如许多人似乎建议的那样,分裂在空白上是一个好主意,因为除了简化的数据之外,任何事情都会失败。

<强>代码:

use strict;
use warnings;
use Text::CSV_XS;

my $csv = Text::CSV_XS->new( {
        sep_char    => "\t",
        binary      => 1,
    });

# get array refs to each row, with appropriate name
# For larger data sets, using an array to hold the array refs would be better
my $name       = $csv->getline(*DATA);
my $gender     = $csv->getline(*DATA);
my $city       = $csv->getline(*DATA);

for (keys @$city) {   # lists the column numbers
    if ($city->[$_] =~ /florida|texas/i) {
        print "$city->[$_]\t$name->[$_]\n";
    }
}

__DATA__
Ron Rob rock    bammy
m   f   m   f
florida Atlanta florida texas

<强>输出:

florida Ron
florida rock
texas   bammy