从txt文件perl中提取倍数列 [英] extract multiples columns from txt file perl

查看:110
本文介绍了从txt文件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.

谢谢

推荐答案

您可以使用

哪个输出:

#Genera columnA columnC columnN 
x1  1   7   2   
x2  5   13  5   
x3  0.1 7   0.4 

如果要完全匹配缩进,则在组合中添加sprintf:

If you want to exactly match your indentation then add sprintf to the mix:

例如:

print join "\t", map { sprintf "%8s", $_} @wanted, "\n";
while ( <$input> ) { 
   my %row;
   @row{@header} = split; 
   print join "\t", map { sprintf "%8s", $_} @row{@wanted}, "\n";
}

然后给出:

 #Genera     columnA     columnC     columnN           
      x1           1           7           2           
      x2           5          13           5           
      x3         0.1           7         0.4    

这篇关于从txt文件perl中提取倍数列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆