将制表符分隔的文本文件合并到单个文件中

时间:2010-12-23 17:23:59

标签: join merge tabs delimited

将文件夹中的所有文件(制表符分隔)加入/合并到单个文件中的最简单方法是什么?它们共享一个唯一的列(主键)。实际上,我只需要在这个主键上组合某个列和链接,因此输出文件将包含每个文件的新列。例如:

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 个答案:

答案 0 :(得分:2)

使用join(1)实用程序

答案 1 :(得分: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)){
      my@col=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";
  }
}
undef@handles; #closes all files
close(OUTFILE);

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