Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi, How do I split a large file of around 20000 lines at a perticular line count of every 500 lines ? Thank you

Replies are listed 'Best First'.
Re: split file at line number
by toolic (Bishop) on Oct 19, 2007 at 14:27 UTC
    If you are on *nix, you could just use the split command:
    split -l 500 file.txt
    This will dump out a lot of new files in your current directory (40 files to be exact: 20000/500).

    If you want to use perl, you can make use of the special variable (INPUT_LINE_NUMBER): $.

Re: split file at line number
by ikegami (Patriarch) on Oct 19, 2007 at 14:53 UTC

    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 $. } }
      Thank you the code. This will be a big help.
Re: split file at line number
by Anonymous Monk on Oct 19, 2007 at 14:45 UTC
    Hey, thank you for the advice. Now I was just told that I have to split a file into multiple files at a particular pattern. Any help?
      Just replace $. % $SPLIT_POINT == 1 with the split condition in the code I provided.