how to improve grep efficiency in perl when the file number is huge

后端 未结 2 477
梦谈多话
梦谈多话 2021-01-23 23:53

I want to grep some log information from the log files located in the following directory structure using perl: $jobDir/jobXXXX/host.log where XXXX is

2条回答
  •  别那么骄傲
    2021-01-24 00:43

    You should search those log file one by one, and scan each log file line by line, instead of reading the output of grep to memory (that could cost lots of memory, and slow your program, even your system):

    # untested script
    
    my $Num;
    foreach my $log (<$jobDir/job*/host.log>) {
        open my $logfh, '<', "$log" or die "Cannot open $log: $!";
        while (<$logfh>) {
            if (m/information/) {
                if(m/\((\d+)\)(.*)\((\d+)\)/) {
                    Output(xxx);
                }
                $Num++;
            }
        }
        close $logfh;
    }
    

提交回复
热议问题