0

我是 perl 新手,我需要开发一个脚本来对目录中的日志进行排序,获取最新文件并仅从日志文件中复制特定内容并将其打印到新文件中。我写了一个下面的 perl 脚本我需要你的宝贵意见。

my($dir) = "C:\Luntbuild_Logs\LACOPBOC LACOPBOC_LASQABOCFC_";
$file = #Here i want to pick up the last generated log file from the above dir
my($newLog) = "new.log";
while (<MYFILE>) {
    print "$line" if $line =~ /> @/;
    print "$line" if $line =~ /ORA-/;
 }
 close (MYFILE);

I want my code to pick up the last generated log file from the dir and print the specific lines and redirect the output to the new log file.

您能否在下面的代码中更正我现在我的 srript 能够排序和打印最新文件(目录中的所有文件都是 txt 文件)但我无法执行该操作

use File::stat;
$dirname = 'C:/Luntbuild_Logs';
$timediff=0;
opendir DIR, "$dirname";
while (defined ($file = readdir(DIR)))
{
                if($file ne "." && $file ne "..")
                {
                                $diff = time()-stat("$dirname/$file")->mtime;
                                if($timediff == 0)
                       {
                                $timediff=$diff;
                                $newest=$file;
                       }
                if($diff<$timediff)
                                       {
                                $timediff=$diff;
                                $newest=$file;
                       }
        }
}
open(FILE,"<$newest");
my(@fprint) = <FILE>;
close FILE;
open(FOUT,">list1.txt") || die("Cannot Open File");
foreach $line (@fprint) {
    print "$line" if $line =~ /> @/;
    print "$line" if $line =~ /ORA-/;
    print FOUT $line;
}
close FOUT;
4

1 回答 1

0

这是获取目录中文件的简单方法(注意正斜杠在 Windows 中有效并且是首选):

chdir "C:/Luntbuild_Logs/LACOPBOC LACOPBOC_LASQABOCFC_";
my @files = <*>;

您可能应该验证您只获得了正确类型的文件。在 Windows 上,您通常可以通过扩展来执行此操作。假设所有文件都以 .log 结尾。将上面的最后一行替换为:

my @files = grep {/\.log$/} <*>;

对于下一步,我建议您看一下这个问题,它提供了许多用于查找文件修改时间的解决方案。

于 2012-08-21T07:59:29.540 回答