admin管理员组

文章数量:1660135

删除文件指定行
对大量文件进行操作
use strict;
#my $dir_to_process="D:\\perl\\oneex";
print "please enter your path(Absolute path):\n";#要进行操作的文件夹
chomp(my $dir_to_process=<>);
my $del_lane_min=3; #需要删除行的下届
my $del_lane_max=9; #需要删除行的上届
opendir DH,$dir_to_process or die "cannot open $dir_to_process:$!\n";
foreach my $my_file (readdir DH){
    next if $my_file eq "." or $my_file eq "..";
    next if -d $my_file;
    if($my_file=~/\.txt$/){#对该文件夹下的txt文件进行操作
         my $new_file_name="new".$my_file;#做备份
         my $maker=0;
         # print "$new_file_name\n";
         open FILE,"$dir_to_process\\$my_file" or die "cannot open file:$!\n";
         open FILEbak,">","$dir_to_process\\$new_file_name" or die "cannot open newfile:$!\n";
             while(<FILE>){
                $maker++;
                print FILEbak $_ if $maker<$del_lane_min || $maker>$del_lane_max;
            }
            close FILE;
            close FILEbak;
    }
}
closedir DH;

本文标签: 批量操作文件Perl