I am new to perl, I need to develop a script to sort the logs in a directory, pickup the latest file and copy only specific contants from the log file and print it to the new file. i have written a below perl script i need your valuable inputs.
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.
can you please correct me on the below code now my srript is able to sort and print the latest file (all files in the dir are txt files) but i am unable to perfom the action
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;