[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-AnyPerl] seperate large file into smaller file



Message-ID: <20001216170651.A190647@linguist.thayer.dartmouth.edu>
References: <3A3B6D07.8DC51824@inet.uni2.dk>
Mime-Version: 1.0
In-Reply-To: <3A3B6D07.8DC51824@inet.uni2.dk>; from allan on Sat, Dec 16,
2000 at 02:24:24PM +0100

On Sat, Dec 16, 2000 at 02:24:24PM +0100, allan wrote:
> #!perl -w
>
> $file = "hit.txt";
> open (FILEHANDLE, $file) or die $!;
> $lines= 0;
> divide_files();
>
> sub divide_files {
>  while (<FILEHANDLE>) {
>   if ($. >= $lines && $. < $lines+1000) {
>    open (NEWFILES, ">$lines.txt") or die $!;
>    print NEWFILES $_;
>    $lines += 1000;
>    divide_files();
>   }
>  }
> }
>

It is a very odd decision to try to use a recursive solution for this
problem.


This script is a modification of one I wrote for someone who wanted to
split a large file by byte count; this version splits by line count
instead.

I use $curr instead of $. in case there are so many lines that $. would
overflow.

#!perl

use strict;

my $in  = shift @ARGV;     # input file
my $out = $in;             # output file base name
my $suf = '000';           # initial suffix to add to base name

my $max  = 1000;           # max number of lines per output file
my $curr = 0;              # number of lines read for current file

open(IN, $in)                                # open input file
  or die "Unable to open $in: $!\n";

open(OUT, '>' . $out . $suf++)               # open first output file
  or die "Unable to open $out: $!\n";

while (<IN>) {                               # read one line
  if ($curr++ == $max) {                     # if at max
    open(OUT, '>' . $out . $suf++)           # open next output file
      or die "Unable to open $out: $!\n";
    $curr = 1;                               # reset current
  }

  print OUT $_;                              # print this line
} # while (<IN>)

__END__

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org