-2

我有一个“users-tb-deleted.txt”文件,它是一个用户列表,由换行符分隔,需要从目录结构中的大约 50 个文件中删除。

如何在不写入 50 个执行相同操作的不同文件句柄参数的情况下打开大量文件并使用以前的列表进行查找和替换到数组中?

当前代码:

use File::Find;
open(FILE, "$path/delete-list.txt") || die "$!\n";
my @user = <FILE>;
close(FILE);
---
opendir(DIR, "/path/to/dir") || die "$!\n";
@docs= grep(/\.uid$/,readdir(DIR));    
foreach $file {
    open (RES, $file) || die "$!\n";
    while(<RES>){
        foreach $user (@user){
            chomp;
            s/$user//ig;
        }
    }
}

有什么想法、想法、建议吗?

4

2 回答 2

0
use File::Find;
use Tie::File;

... # define $path and @dirs and &some_condition;

open(FILE, "$path/delete-list.txt") || die "$!\n";
my @user = <FILE>;
close(FILE); 
my $regex = join "|", @user;

find(\&wanted, @dirs);

sub wanted {
    my $full_path = $File::Find::name;
    next if -d $full_path;
    next unless &some_condition($full_path);

    tie my @lines, $full_path or die $!;

    for my $line (@lines) {
        $line =~ s/($regex)//ig;
    }

    untie @lines;
}
于 2013-03-25T22:18:54.583 回答
0

请注意

opendir(DIR, "/path/to/dir") || die "$!\n";
@docs= grep(/\.uid$/,readdir(DIR));

更容易阅读为

@docs = glob( '/path/to/dir/*.uid' );
于 2013-09-24T13:35:47.737 回答