1
请帮忙。Perl,提取特定列
我有两个文件(file1和file2)。我想从file2中提取ID列在file1中的列。这些都是大文件,有成千上万的列和行。
file1的
Id123B
Id124A
Id125A
file2的
Code sex id123B id127 id125A
期望的输出文件:
code sex id123B id125A
以下是我已经尝试的代码,但它失败。
!/usr/bin/perl
use strict;
use warnings;
open my $IN, "file2" or die $!;
my $header = <$IN>;
my %sampleID = map { /(.*?)\t/; $1 => 1 } <$IN>;
close($IN);
open $IN, "file1" or die $!;
$header = <$IN>;
my @samples = split /\t/, $header;
my @cols = grep { exists $sampleID{$samples[$_]} } 0..$#samples;
while(<$IN>){
chomp;
my @line = (split /\t/)[@cols];
print join("\t", @line), "\n";
}
非常感谢您的帮助。我刚刚运行它,它不打印任何输出。数据格式也许? –
@ El.h可能对我很有用。检查列名称。 – choroba