我要报价一个Perl解决方案,即使它没有被标记,因为Perl非常适合解决这个问题。
如果我知道你想做什么,你必须要基于在数据文件中的行的位置分割成5个独立文件的单个文件:
the first line in the data file goes to file 1
the second line in the data file goes to file 2
the third line in the data file goes to file 3
...
既然你已经有了在文件中的行位置,你并不需要标识符列(尽管如果你想要的话,你可以追求解决方案)。
相反,你可以打开5个句柄和简单地替代它处理你写:
use strict;
use warnings;
my $datafilename = shift @ARGV;
# open filehandles and store them in an array
my @fhs;
foreach my $i (0 .. 4) {
open my $fh, '>', "${datafilename}_$i"
or die "$!";
$fhs[$i] = $fh;
}
# open the datafile
open my $datafile_fh, '<', $datafilename
or die "$!";
my $row_number = 0;
while (my $datarow = <$datafile_fh>) {
print { $fhs[$row_number++ % @fhs] } $datarow;
}
# close resources
foreach my $fh (@fhs) {
close $fh;
}
使用一些VIM脚本魔法 – deathangel908