$in = $ARGV[0];
%dict = ();
$usage = '
Usage:
1. perl file_splitter.pl <file.txt>
2. perl file_splitter.pl <file.txt> <column_index(1_Start)>
';
if($#ARGV == -1 ){
die($usage);
}
if($#ARGV == 0){
$in = $ARGV[0];
print STDOUT "请输入切分列(从1开始编号):";
$col = <STDIN>;
}
if($#ARGV == 1){
$in = $ARGV[0];
$col = $ARGV[1];
}
print '文件切分即将开始:'.
"\n切分文件:$in\n".
"切分列:$col\n"
."请确认(Y/N):";
$yesOrNo = <STDIN>;
chomp $yesOrNo;
while($yesOrNo ne 'Y' and $yesOrNo ne 'N' and $yesOrNo ne 'y' and $yesOrNo ne 'n'){
print "请确认(Y/N):";
$yesOrNo = <STDIN>;
chomp $yesOrNo;
}
if($yesOrNo eq 'N' or $yesOrNo eq 'n'){
die("退出程序");
}
$col --;
open RH,$in or die "$!";
$header = <RH>;
while($ln = <RH>){
@t = split(/\t/,$ln);
if($dict{$t[$col]}){
push @{$dict{$t[$col]}},$ln;
}else{
$dict{$t[$col]} = [$ln];
}
}
close RH;
while(my($xq,$data) = each %dict){
open WH,">".$xq . "_S.txt";
print WH $header;
for($i = 0; $i < @{$data}; $i ++){
print WH $data->[$i];
}
close WH;
}
txt根据某一列进行文件切分
点赞
收藏