Split file: Difference between revisions

From RAJ INFO
Jump to navigation Jump to search
m New page: * Cut a Text File into Pieces <pre> #!/usr/bin/perl -w #---------------------------- # Description # Requirements: POSIX module. Comes handy with pearl installation # Usage: Type filename...
 
mNo edit summary
 
Line 1: Line 1:
* Cut a Text File into Pieces
* Cut a Text File into Pieces
<pre>
<source lang=perl>
#!/usr/bin/perl -w
#!/usr/bin/perl -w


Line 43: Line 43:


#END
#END
</pre>
</source>
[[Category:Bioinformatics]]

Latest revision as of 18:48, 3 January 2010

  • 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