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 机器。

4

2 回答 2

2

使用join(1)实用程序

于 2010-12-23T17:28:24.960 回答
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";
于 2010-12-24T17:25:15.317 回答