#!/usr/bin/perl
use warnings;
use strict;
use POSIX qw/ceil floor/;
use Benchmark;
my $argc = @ARGV;
if ($argc !=2) { die "Usage: truncJnl.pl <journal_name> <split_number>\n"; }
my $jnl = $ARGV[0];
my $splits = int($ARGV[1]);
my $start = new Benchmark;
open(IN,"$jnl") or die "Unable to open $jnl: $!\n";
my $count = 0;
while(<IN>) { $count++; }
close(IN);
print "Total line count of $jnl: " . $count . "\n";
print "The line of each $jnl is " . floor($count/int($splits)) . "\n";
my $chunk = floor($count/int($splits));
open(IN,"$jnl") or die "Unable to open $jnl: $!\n";
my ($jnl_ct,$chunk_bk) = 0;
my $line_ct = 0;
while(<IN>) {
if($line_ct == 0 || $chunk_bk == 0) {
if($line_ct == 0) {
#pass
} else {
close(OUT);
print "Done with $jnl.$jnl_ct\n";
}
$chunk_bk = $chunk;
$jnl_ct++;
open(OUT,"> $jnl.$jnl_ct") or die "Unable to open $jnl.$jnl_ct\n";
}
print OUT $_;
$line_ct++;
$chunk_bk--;
}
close(IN);
my $end = new Benchmark;
my $diff = timediff($end, $start);
print "Time taken was ", timestr($diff, 'all'), " seconds";