如果你找不到合适的替代品,在 Perl 中做起来并不难(它会表现得很好):
#!/usr/bin/env perl
use strict;
use warnings;
# Configuration items - could be set by argument handling
my $prefix = "rs."; # File prefix
my $number = 1; # First file number
my $width = 4; # Number of digits to use in file name
my $rx = qr/^\+$/; # Match regex
my $limit = 3; # 50,000 in real case
my $quiet = 0; # Set to 1 to suppress file names
sub next_file
{
my $name = sprintf("%s%.*d", $prefix, $width, $number++);
open my $fh, '>', $name or die "Failed to open $name for writing";
print "$name\n" unless $quiet;
return $fh;
}
my $fh = next_file; # Output file handle
my $counter = 0; # Match counter
while (<>)
{
print $fh $_;
$counter++ if (m/$rx/);
if ($counter >= $limit)
{
close $fh;
$fh = next_file;
$counter = 0;
}
}
close $fh;
这远非单线;我不确定这是否是一个优点。应该配置的项目组合在一起,例如,可以通过命令行选项进行设置。您最终可能会得到一个空文件;您可以发现它并在必要时将其删除。你需要第二个柜台;现有的是一个“匹配计数器”,但您还需要一个行计数器,如果行计数器为零,您将删除最后一个文件。您还需要名称才能将其删除……虽然很繁琐,但并不难。
给出输入(基本上是样本数据的两个副本),repsplit.pl
(重复拆分)的输出如下所示:
$ perl repsplit.pl data
rs.0001
rs.0002
rs.0003
$ cat data
entry 1
some more
+
entry 2
some more
even more
+
entry 3
some more
+
entry 4
some more
+
entry 1
some more
+
entry 2
some more
even more
+
entry 3
some more
+
entry 4
some more
+
$ cat rs.0001
entry 1
some more
+
entry 2
some more
even more
+
entry 3
some more
+
$ cat rs.0002
entry 4
some more
+
entry 1
some more
+
entry 2
some more
even more
+
$ cat rs.0003
entry 3
some more
+
entry 4
some more
+
$