【发布时间】:2012-08-21 07:19:50
【问题描述】:
我是 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;
【问题讨论】:
-
@tripleee,然而,这个问题结合了几个不同的任务。这只是其中之一。
-
也不要同时发布多个问题。
-
始终在脚本开头使用
use strict;和use warnings;。
标签: perl