James Kipp wrote:
I am working on a Windows NT box and I don't have the luxury of any file
splitting utilities. We have a data file with fixed length records. I was
wondering the most efficient way of splitting the file into 5 smaller files.
Thought ( Hoping :-) ) some one out there may have done something like this.


Thanks !!
#!/usr/bin/perl -w

use strict;

# call new() with named args found in init() to override defaults
my( $fSpliter ) = Text::FileSplitter->new();

$fSpliter->split();

print( "done!\n" );

package Text::FileSplitter;
use strict;
use IO::File;

sub new {
  my($class, %args) = @_;
  my($self) = bless( { %args }, $class );
  $self->init();
  return( $self );
}

sub init {
  my($self) = shift(); my($filehandles) = [];

  $self->{ file } ||= './splitfile.txt';
  $self->{ output_prefix } ||= ( ($self->{ file } =~ /(\w+)/) and $1 );
  $self->{ file_count }  ||= 5;
  $self->{ record_length }  ||= 10;

  $self->{ fh } = IO::File->new( "< $self->{ file }" )
    or die("open $self->{ file }: $!");

  foreach ( 1 .. $self->{ file_count } ) {
    push(
      @{ $filehandles },
      IO::File->new("> $self->{ output_prefix }.$_")
    );
  }
  $self->{ ofh } = $filehandles;

}

sub split {
  my($self) = shift(); my($buffer);
  my($counter) = 0;
  while ( sysread $self->{ fh }, $buffer, $self->{ record_length } ) {
    $self->{ ofh }[ $counter % $self->{ file_count } ]->print( $buffer );
    $counter++;
  }
}

HTH

Todd W.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to