从txt文件perl中提取倍数列 [英] extract multiples columns from txt file perl
问题描述
我有一个这样的txt文件:
I have a txt file like this:
#Genera columnA columnB columnC columnD columnN
x1 1 3 7 0.9 2
x2 5 3 13 7 5
x3 0.1 0.8 7 1 0.4
并且我想提取X个确定的列数,只是假设我们想要columnA,columnC和columnN(这可以是具有1、2、20、100或更多列的矩阵)以及我要打印的内容(这个例子只有3个,但可能更多):
and I want to extract X determinate number of columns, just suppose that we want columnA, columnC and columnN (this could be a matrix with 1, 2, 20, 100 or more columns) and What I want to print OUT (this example is just 3 but could be more):
#Genera columnA columnC columnN
x1 1 7 2
x2 5 13 5
x3 0.1 7 0.4
我尝试过
#!/usr/bin/perl
use strict;
use warnings;
my @wanted_fields = qw/columnA columnC columnN/;
open DATA, '<', "columns.txt" or die "cant open file\n";
my @datain = <DATA>;
close DATA;
my (@unit_name, $names, @lines, @conteo, @match_names, @columnas);
foreach (@datain){
if ($_=~ m/^$/g) { next; }
elsif ($_=~ m/#Genera/g) { $names= $_; }
else { push @lines, $_ }
}
@unit_name = split (/\t/, $names);
shift @unit_name;
my $count =0;
foreach (@wanted_fields){
my $unit_wanted =$_;
chomp $unit_wanted;
foreach (@unit_name){
if ($_ =~ m/$unit_wanted/g){
$count++;
push (@conteo, $count);
push (@match_names, $_);
}
}
}
foreach (@lines){
chomp;
@columnas = split (/\t/, $_);
#push @xx, $columnas[0][3];
}
我用计数来确定要提取的列,但是在这种情况下,数字2并不对应于columnC,数字3并不对应于columnN……这是选择任何给定列的任何简单方法,在这种情况下,我只需要3列,但取决于情况可能是1,2、5、10、100或更多列.
I used the count to determinate the column to extract but in this case the number 2 do no correspond to columnC and 3 do not correspond to columnN well...... it is a any simple way to select any given columns, in this case I just want 3 but depend of the case could be 1,2 5, 10, 100 or more columns.
谢谢