User Tools

Site Tools


tutorials:perl:write_lines_to_files.pl
write_lines_to_files.pl
# write_lines_to_files.pl
# Obtain an input file and 
# an output directory from the command line. 
# Produce one output file for each line in 
# the input file, use the line number 
# (i.e., 1, 2, 3, etc) as the filenames.
 
my $in_file = shift;
my $out_dir = shift;
 
system "mkdir -p $out_dir" unless -e $out_dir;
 
my $count_line = 0;
open IN, "<$in_file";
while ( my $line = <IN> ) {
	$count_line++;
	my $out_file = $out_dir . $count_line;
	open OUT, ">$out_file";
	print OUT "$line";
	close OUT;
}
close IN;
tutorials/perl/write_lines_to_files.pl.txt · Last modified: 2012/06/15 00:36 by chkuo