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
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
I have tried
#!/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];
}
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.
Thanks