• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Finding the latest entry.

Hi guys!

I have a list of systems in a file called systems.txt

What I need to do is:
1) Read the systems.txt file (done)
2) Connect to each machine (done)
3) Check for the existence of a file (eg. \\\\$systemname\\d\$\\bin\\updatelog.txt)
4) If this file exists, then read the file, and find the following...

14/03/2007       8:44AM        Administrator        TESTXP           Update process successful
16/04/2007       10:26PM       Adminstrator         TESTXP          Replacing file.......
19/06/2007       8:44AM        Administrator        TESTXP           Update process successful
20/04/2007       03:26AM       Adminstrator         TESTXP          Replacing file.......
20/07/2007       8:44AM        Administrator        TESTXP           Update process successful

Out of the above, I need to find the LATEST (by date/time) for the field denoted by "Update process successful". If the latestentry says "Update process successful", then write the $system name, along with the date/time (eg.computer variable for each system in systems.txt) to a file called "success.txt". If it says "Update failed" (and there can be multiple entries as well for this, so only the latest) to a file called "failed.txt".

Any help greatly appreciated.

0
Simon336697
Asked:
Simon336697
  • 5
  • 4
2 Solutions
 
Adam314Commented:
use Time::Local;

my $systemname='testsystem';
my $fn="\\\\$systemname\\d\$\\bin\\updatelog.txt";

die "File does not exist" unless -e $fn;

open(IN,"<$fn") or die "Could not open file: $!\n";
my $latesttime=0;
my $latestmsg='';
while(<IN>) {
      next unless /(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+)(\w)\s+(\w+)\s+(\w+)\s+(.*)$/;
      my $h=$4;
      $h+=12 if $6 eq 'P';
      my $thistime = timelocal(0, $5, $h, $1,$2-1,$3);
      my $msg=$9;
      
      if($thistime>$latesttime){
            $latesttime=$thistime;
            $latestmsg=$msg;
      }
}
close(IN);

if($latestmsg eq 'Update process successful') {
      open(OUT,">>success.txt") or die "Could not open success: $!\n";
      print OUT "$systemname\n";
      close(OUT);
}
elsif($latestmsg eq 'Update failed') {
      open(OUT,">>failed.txt") or die "Could not open failed: $!\n";
      print OUT "$systemname\n";
      close(OUT);
}
else {
      print "Latest message is neither success or failed\n";
}
0
 
Simon336697Author Commented:
Hi Adam!

My query resulted in the following:

------------------------------------------------------- updatelog.txt
14/03/2007       8:44AM        Administrator        TESTXP           Update process successful
16/04/2007       10:26PM       Adminstrator         TESTXP          Replacing file.......
19/06/2007       8:44AM        Administrator        TESTXP           Update process successful
20/04/2007       03:26AM       Adminstrator         TESTXP          Replacing file.......
20/07/2007       8:44AM        Administrator        TESTXP           Update process successful


C:\>update.pl
Latest message is neither success or failed

0
 
ozoCommented:
next unless /(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+)(\w)\w*\s+(\w+)\s+(\w+)\s+(.*)$/;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Simon336697Author Commented:
Guys that works with ozos line included.

Guys I understand all of what youve done except:

      my $h=$4;
      $h+=12 if $6 eq 'P';
      my $thistime = timelocal(0, $5, $h, $1,$2-1,$3);
      my $msg=$9;

Not sure about the above (even tho it works)
0
 
ozoCommented:
$h+=12 if $6 eq 'P';
adds 12 hours in the afternoon


perldoc Time::Local

NAME
       Time::Local - efficiently compute time from local and GMT time

SYNOPSIS
           $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
           $time = timegm($sec,$min,$hour,$mday,$mon,$year);

DESCRIPTION
       These routines are the inverse of built-in perl functions localtime()
       and gmtime().  They accept a date as a six-element array, and return
       the corresponding time(2) value in seconds since the system epoch (Mid-
       night, January 1, 1970 GMT on Unix, for example).  This value can be
       positive or negative, though POSIX only requires support for positive
       values, so dates before the system's epoch may not work on all operat-
0
 
Simon336697Author Commented:
Ok gotcha ozo...One last one then points if thats ok.

The $6 in:

$h+=12 if $6 eq 'P';

0
 
ozoCommented:
came from the 6th () which was (\w)
0
 
Simon336697Author Commented:
my $h=$4;

Why was this set to $4?

Sorry no more now.
0
 
ozoCommented:
$4 was the (\d+) in \s+(\d+):
0
 
Simon336697Author Commented:
Thanks ozo!

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now