2010-12-23 109 views
2

將文件夾(製表符分隔)中的所有文件加入/合併到單個文件中的最簡單方法是什麼?他們都共享一個獨特的列(主鍵)。實際上,我只需要在該主鍵上組合一個特定的列和鏈接,因此輸出文件將爲每個文件包含一個新列。例如:將製表符分隔的文本文件合併到單個文件中

KEY# Ratio1 Ratio2 Ratio3 
1  5.1  4.4  3.3 
2  1.2  2.3  3.2 
etc.... 

有在每一個我並不需要在輸出文件合併文件多等欄目,我只需要通過獨特的鍵列連接這些「率」列。

我運行的是OS X Snow Leopard,但可以訪問幾臺Linux機器。

回答

2

其實我花了一些時間來學習Perl和解決我自己的問題。我想我會分享源代碼,如果有人有類似的問題需要解決。

#!/usr/bin/perl -w 

#File: combine_all.pl 
#Description: This program will combine the rates from all "gff" files in the current directory. 

use Cwd; #provides current working directory related functions 
my(@handles); 

print "Process starting... Please wait this may take a few minutes...\n"; 

unlink"_combined.out"; #this will remove the file if it exists 

for(<./*.gff>){ 
    @file = split("_",$_); 
    push(@files, substr($file[0], 2)); 
    open($handles[@handles],$_); 
} 

open(OUTFILE,">_combined.out"); 

foreach (@files){ 
    print OUTFILE"$_" . "\t"; 
} 

#print OUTFILE"\n"; 

my$continue=1; 

while($continue){ 
    $continue=0; 

    for my$op(@handles){ 
    if($_=readline($op)){ 
     [email protected]=split; 
     if($col[8]) { 
     $gibberish=0; 
     $col[3]+=0; 
     $key = $col[3]; 
     $col[5]+=0; #otherwise you print nothing 
     $col[5] = sprintf("%.2f", $col[5]); 
     print OUTFILE"$col[5]\t"; 
     $continue=1; 
     } else { 
     $key = "\t"; 
     $continue=1; 
     $gibberish=1; 
     } 
    }else{ 
     #do nothing 
    } 
    } 
    if($continue != 0 && $gibberish != 1) { 
    print OUTFILE"$key\n"; 
    } else { 
    print OUTFILE"\n"; 
    } 
} 
[email protected]; #closes all files 
close(OUTFILE); 

print "Process Complete! The output file is located in the current directory with the filename: _combined.out\n"; 
2

使用join(1)實用

+0

我正在研究使用此實用程序,但注意到它是用於組合兩個文件,而不是文件夾中的所有文件。我不確定如何在不編寫一段代碼的情況下利用此工具來使其正常工作。 – DaRkMuCk 2010-12-23 18:02:19

相關問題