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