#!/usr/bin/perl
#--------------------------------------------------#
# SPLIT_UI.pl - Used to split large files #
# be it log file, or just text files. Does so by #
# the amount of lines [you specify] in the file. #
#--------------------------------------------------#
#----------------------Coded By--------------------#
# ______ _ _____ #
# | ___ \ | | _ | #
# | |_/ / | ___ _ __ | |/' |_ __ #
# | __/| |/ _ \ '_ \| /| \ \/ / #
# | | | | __/ | | \ |_/ /> < #
# \_| |_|\___|_| |_|\___//_/\_\ #
# #
#--------------------------------------------------#
# Important Notice Below, Please Read... #
# ====================================== #
# 1) The Terms and Conditions to this program are #
# located at the bottom of the code, along with #
# all the things stated here. #
# #
# 2) You may modify this source to fit your needs. #
# It may be modified in any shape, or form... #
# #
# 3) You may sell this source to people in both #
# physical, and digital forms. #
# #
# 4) This source IS owned by the original creator #
# PaTcH, and by my/his wishes, please leave this #
# and all the above (Including the ASCII) in this #
# programs source code. #
#--------------------------------------------------#
#-------Please Read Notice At End Of Source--------#
#--------------------------------------------------#
# /\/\
#(O.o) This is Bunny. Copy Bunny into your signature
#(> <) to help him on his way to world domination.
use warnings;
use strict;
use Getopt::Long;
my ( $FILENAME, $LINEBREAK, $start_time, $exec_time, $lineNum, $result ) =
undef;
my ( $nameCount, $linecount ) = 0;
my ( $overwrite, $linesonly, $sizecheck ) = "";
$result = GetOptions(
"linesonly" => \$linesonly,
"overwrite" => \$overwrite,
"sizecheck" => \$sizecheck );
if ( $linesonly ) {
&lines_only($FILENAME);
my @lines = <FILE>;
$lineNum = @lines;
print $lineNum;
}
elsif ( $overwrite ) {
&file_num_check( $FILENAME, $LINEBREAK );
my @lines = <FILE>;
$lineNum = @lines;
if ( $LINEBREAK > $lineNum ) {
print "Error, you\'ve entered a line number larger then the total lines in the file.\n";
print "Please re\-run script\n";
exit;
}
$start_time = time();
while (@lines) {
$linecount++;
if ( $linecount % $LINEBREAK == 0 ) {
$nameCount++;
open( FILE2, ">$FILENAME\_$nameCount.log" )
|| die "Failed to open :$!\n";
for ( my $i = 0 ; $i < $LINEBREAK ; $i++ ) {
my $shifter = shift(@lines);
if ($shifter) {
print FILE2 $shifter;
}
}
}
}
}
elsif ( !$linesonly ) {
&file_num_check( $FILENAME, $LINEBREAK );
my @lines = <FILE>;
$lineNum = @lines;
if ( $LINEBREAK > $lineNum ) {
print "Error, you\'ve entered a line number larger then the total lines in the file.\n";
print "Please re\-run script\n";
exit;
}
$start_time = time();
while (@lines) {
$linecount++;
if ( $linecount % $LINEBREAK == 0 ) {
$nameCount++;
if ( !-e "$FILENAME\_$nameCount.log" ) {
open( FILE2, ">$FILENAME\_$nameCount.log" )
|| die "Failed to open :$!\n";
for ( my $i = 0 ; $i < $LINEBREAK ; $i++ ) {
my $shifter = shift(@lines);
if ($shifter) {
print FILE2 $shifter;
}
else { last }
}
close(FILE2);
}
else {
for ( my $i = 0 ; $i < $LINEBREAK ; $i++ ) {
my $shifter = shift(@lines);
if ( !$shifter ) { last }
}
}
}
}
$exec_time = time() - $start_time;
print qq {
The Log had a total of $lineNum lines.
It was split into $nameCount files,
And took a total execution time of $exec_time seconds.
};
}
#------------------------
sub lines_only() {
print "Please Give a filename : ";
$_[0] = <STDIN>;
chomp( $_[0] );
if ( !-e $_[0] ) {
print "Invalid Filename :$!\n";
exit;
}
open( FILE, $_[0] ) || die "Failed to Open : $!\n$@";
}
sub file_num_check() {
print "Please Give a filename : ";
$_[0] = <STDIN>;
chomp( $_[0] );
if ( !-e $_[0] ) {
print "Invalid Filename :$!\n";
exit;
}
open( FILE, $_[0] ) || die "Failed to open :$!\n";
if ( $_[0] =~ /(.+)\..+/ ) {
$_[0] = "$1";
}
print "And number of lines per file? : ";
$_[1] = <STDIN>;
if ( $_[1] !~ /^\d+$/ ) {
print "Non-numeric\n";
exit;
}
}
#--------------------------------------------------------------------------------#=====================#
# This program is free software; you can redistribute it and/or modify # Notes : #
# it under the terms of the GNU General Public License as published by # Megabyte found my #
# the Free Software Foundation; either version 2 of the License, or # problem with the #
# (at your option) any later version. # execution time so #
# # he deserves credit #
# This program is distributed in the hope that it will be useful, # for that. Also, he #
# but WITHOUT ANY WARRANTY; without even the implied warranty of # reminded me to see #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # if the file(s) are #
# GNU General Public License for more details. # already made. #
# # I ended up making #
# You should have received a copy of the GNU General Public License # it over-write them #
# along with this program; if not, write to the Free Software # if the user said #
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # to do so. Still :P #
#--------------------------------------------------------------------------------#=====================#
# PaTcH (R. S.) Copyright (C) 2006-2008