Created
March 10, 2011 21:41
-
-
Save stephenturner/864990 to your computer and use it in GitHub Desktop.
splitbycol.pl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Thursday, March 10, 2011 | |
# Stephen D. Turner | |
chomp(my $pwd = `pwd`); | |
my $help = "\nUsage: $0 <file to split> <# columns in each split>\n\n"; | |
die $help if @ARGV!=2; | |
#sub trim($); | |
$infile = $ARGV[0]; | |
chomp($ncol = `head -n 1 $infile | wc -w`); | |
$ncol = trim($ncol); | |
$start=1; | |
$inc = $ARGV[1]; | |
$end = $start+$inc-1; | |
die "\nSplit size >= number of columns\n\n" if $inc>=$ncol; | |
for($i=1 ; $i<$ncol/$inc +1 ; $i++) { | |
if ($end>$ncol) {$end=$ncol;} | |
`cut -f $start-$end $infile > $infile.$i.txt`; | |
$start += $inc; | |
$end += $inc; | |
} | |
# function to trim leading and trailing whitespace | |
sub trim($) | |
{ | |
my $string = shift; | |
$string =~ s/^\s+//; | |
$string =~ s/\s+$//; | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment