0

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;
user1587062
  • 5
  • 1
  • 4
  • possible duplicate of [How can I find the newest created file in a directory?](http://stackoverflow.com/questions/328673/how-can-i-find-the-newest-created-file-in-a-directory) – tripleee Aug 21 '12 at 07:53
  • @tripleee, this question combines several different tasks, however. That is only one of them. – dan1111 Aug 21 '12 at 08:02
  • Don't post multiple questions in one, either. – tripleee Aug 21 '12 at 08:10
  • ALWAYS `use strict;` and `use warnings;` at the begining of your scripts. – Toto Aug 22 '12 at 11:29

1 Answers1

0

Here's a simple way to get the files in a directory (note forward slashes work in Windows and are preferred):

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

You should probably verify that you are only getting the correct type of file. On Windows you can generally do this by the extension. Suppose all of your files end with .log. Replace the last line above with:

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

For the next step, I suggest you look at this question, which provides a number of solutions for finding the time a file was modified.

Community
  • 1
  • 1
dan1111
  • 6,576
  • 2
  • 18
  • 29