in reply to split file at line number

In Perl,

#!/usr/bin/perl # Usage: # split.pl numlines file [file [file [...]]] use strict; use warnings; my $SPLIT_POINT = shift(@ARGV); my $fh; my $part = 'aaaa'; while (<>) { if ($. % $SPLIT_POINT == 1) { # Time to start a new output file. undef $fh; } if (!defined($fh)) { # Or whatever my ($file, $ext) = $ARGV =~ /^(.*?)((?:\.\w*)?)\z/s; $file .= sprintf('[%s]%s', $part++, $ext); open($fh, '>', $file) or die("Unable to create part file \"$file\": $!\n"); } print $fh $_; if (eof(ARGV)) { # Finished processing one input file. undef $fh; # Start a new output file. $part = 'aaaa'; # Reset part number. close(ARGV); # Reset $. } }

Replies are listed 'Best First'.
Re^2: split file at line number
by Anonymous Monk on Oct 19, 2007 at 15:22 UTC
    Thank you the code. This will be a big help.