由于结果文件不完全匹配,因此regexp需要进行一些调整。以:perl scriptname.pl<sample.txt的形式运行它,就可以得到块文件。
#!/usr/bin/perl -w
use strict;
use IO::File;
my $all = join('', (<STDIN>));
my (@pieces) = ($all =~ m%([IZO]\(.*?\)\{.*?\r\n\}\r\n)%gsx);
my $n = 1;
my $FH;
foreach my $P (@pieces) {
if ($P =~ m%^I%) {
undef $FH;
$FH = IO::File->new(sprintf("> chunk%d", $n));
$n++;
}
print $FH $P;
}
#!/usr/bin/env python
import sys
def split(filename, size=100, outputPrefix="xxx"):
with open(filename) as I:
n = 0
FNM = "{}{}.txt"
O = open(FNM.format(outputPrefix, n), "w")
toWrite = size*1024*1024
for line in I:
toWrite -= len(line)
if line[0] == 'I' and toWrite < 0:
O.close()
toWrite = size*1024*1024
n += 1
O = open(FNM.format(outputPrefix, n), "w")
O.write(line)
O.close()
if __name__ == "__main__":
split(sys.argv[1])