简单拆分文本

今天被老师喊过来帮个小忙,有个文件太大了,把它分了。

在这里留个备份吧。

#!/usr/bin/perl -w
use strict;
use warnings;
# 拆分文本文件
# 可以建个文件夹
# 目前是 csv

my $csv = 'xxx.csv';
print "File read from is '$csv'.\n";
my $in_fh = read_text($csv);

# 文件数目
my $num_of_files = 15;

# 行数
my $total_lines = 12465682;        # 总行数
# 各文本大致行数
my $lines = int($total_lines / $num_of_files);

my $count = 0;
my $n = 1;
my $out_file = "xxx_$n.csv";
print "Create $out_file ...\n";
my $w_fh = write_text($out_file);

while (<$in_fh>) {
	if ($n < $num_of_files and $count == $lines) {
		$n++;
		$count = 0;
		close $w_fh;
		$out_file = "xxx_$n.csv";
		print "Create $out_file ...\n";
		$w_fh = write_text($out_file);
	}
	print $w_fh $_;
	$count++;
}
close $w_fh;
close $in_fh;

print "Done!\n";
exit;

#====================================================
# subroutions

# 读取文本文件
sub read_text {
	my $filename = shift;
	my $fh;
	open $fh, '<', $filename or die "Cannot open file '$filename': $!";
	return $fh;
}

# 写文本文件
sub write_text {
	my $filename = shift;
	my $fh;
	open $fh, '>', $filename or die "Cannot open file '$filename': $!";
	return $fh;
}

你可能感兴趣的:(perl,拆分文本)