2010-12-23 89 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

相关问题