Split file

From RAJ INFO
Revision as of 18:48, 3 January 2010 by Raj (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
  • Cut a Text File into Pieces
#!/usr/bin/perl -w

#----------------------------
# Description
# Requirements: POSIX module. Comes handy with pearl installation
# Usage: Type filename and number of pieces separated by a space
# Author: Rajkumar
#--------------------------------

# Use of functions
	use strict;
	use POSIX;

# Variables

	my $argcount = @ARGV;
	if ($argcount !=2 ) {die "Usage: Type filename and number of pieces!\n";}
	my ($file,$cut) = @ARGV;
	my $piece = $_;

# Function
	chomp($file);
	chomp($cut);
	
	my $total = `cat $file|wc -c`;
	my $math = $total/$cut;
	$math = ceil($math);
	my $read = `cat $file`;
	
	my $start = 0;
	for (my $i = 0; ++$i;) {
		$piece = substr($read, $start, $math);

		open(PIECE, ">".$file."_".$i."txt" );
		print PIECE $piece;
		close PIECE;

		$start = $start+$math;
		if ($i eq $cut) {last;}
	}

#END