
每个.txt文件都是以管道分隔的,只包含20条记录.记录6是包含确定将文件移动到哪个目录所需的信息的记录.
记录示例:
A|CHNL_ID|4
在这种情况下,文件将被移动到/ out / 4.
此脚本的处理速度为每小时80,000个文件.
有关如何加快速度的建议吗?
opendir(DIR,$dir) or dIE "$!\n";while ( defined( my $txtfile = readdir DIR ) ) { next if( $txtfile !~ /.txt$/ ); $cnt++; local $/; open my $fh,'<',$txtfile or dIE $!,$/; my $data = <$fh>; my ($channel) = $data =~ /A\|CHNL_ID\|(\d+)/i; close($fh); move ($txtfile,"$outDir/$channel") or dIE $!,$/;}closedir(DIR);解决方法 尝试类似的东西: print localtime()."\n"; #to find where time is spentopendir(DIR,$dir) or dIE "$!\n";my @txtfiles = map "$dir/$_",grep /\.txt$/,readdir DIR;closedir(DIR);print localtime()."\n";my %fileGroup;for my $txtfile (@txtfiles){ # local $/ = "\n"; #\n or other record separator open my $fh,$txtfile or dIE $!; local $_ = join("",map {<$fh>} 1..6); #read 6 records,not whole file close($fh); push @{ $fileGroup{} },$txtfile if /A\|CHNL_ID\|(\d+)/i or dIE "No channel found in $_";}for my $channel (sort keys %fileGroup){ moveGroup( @{ $fileGroup{$channel} },"$outDir/$channel" );}print localtime()." finito\n";sub moveGroup { my $dir=pop@_; print localtime()." <- start $dir\n"; move($_,$dir) for @_; #or something else if each move spawns sub process #rename($_,$dir) for @_;} 这将作业分成三个主要部分,您可以在每个部分计算时间以查找花费大部分时间的位置.
总结以上是内存溢出为你收集整理的regex – Perl程序,可以有效地处理目录中的500,000个小文件全部内容,希望文章能够帮你解决regex – Perl程序,可以有效地处理目录中的500,000个小文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)