Hi Perl Gurus,

I have a text which look like

20 Dec 2010 | Completed | ABC
20 Dec 2010 | Completed | DEF
20 Dec 2010 | Completed | GHI
19 Dec 2010 | Completed | JKL

You can see that the file have three columns first column is date column second column is a Status and the third column is a job name column now i want to check the first column of the above file (i.e., date) and wants to compare the date of file with current system date or day-1 , day-2 or day-3 date if the date in file is equal to the current date or file date is equal to day-1, day-2 or day-3 date the add 1 in the total successfull job and if the date is not equal to current date or if date not equal to day-1, day-2 or date-3 date then change the status of column two (i.e., completed) to 'Did not Run'after the changing the status of job to 'Did not run' add 1 in the counter of failed jobs furhter more i also want the name of jobs there were failed i.e., the values of column3.


and here is for what kind of output i am looking for

Total Successfull jobs = ''
Total Failed Jobs = ''
Name of Failed jobs are '','','',''

Although i have written few lines but i am unable to get this done please help me out to complete this program.

Thanks in advance

The bulk of this job can be pulled with a simple file iteration

open my $FH, '<', $filepath || die "Cannot open file $filepath - $!";
my (@completed, @not_completed);
while (<$FH>) {
    chomp;
    my @status = split /\|/;

    if ($status[1] =~ /Completed/i) {
        push @completed, \@status;
    } else {
        push @not_completed, \@status;
    }
}

print "Total successful jobs = " . scalar(@completed) . "\n";
print "Total failed jobs = " . scalar(@not_completed) . "\n";
print "Name of Failed jobs are ";
foreach (@not_completed) {
    print @{$_}[2].',';
}

However, for the date parsing, I would be able to give you any assistance if you can clarify what exactly do you want the output to be (some examples would come in handy

Hi erezschatz,

Thanks for your response but the above code is giving me a wrong output because in file currently i have 10 jobs and all jobs were successfull completed but your code is giving me output like

Total successfull jobs = 10
Total failed jobs = 9
Name of Failed jobs are

More over the provided code is not a desired one because i have a file which contains three columns start date (the date is sotored in file as a string) job status and job name further more that file have not such kind of functionality to store the status of failed jobs the file only contains the status of successfull jobs or the jobs completed with warnings. let me explain the file in more details.

for example i have 100 jobs which runs between every friday,saturday and sunday now the out put of file will be

28 Jan 2011 | Completed | ABC
28 Jan 2011 | Completed | DEF
28 Jan 2011 | Completed | GHI
28 Jan 2011 | Completed with warnings | JKL
28 Jan 2011 | Completed | LMN
28 Jan 2011 | Completed | OPQ
28 Jan 2011 | Completed with warnings| RST
21 jan 2011 | Completed | UVW
21 jan 2011 | Completed | XYZ
21 jan 2011 | Completed | MVU
21 jan 2011 | Completed | QPO
28 Jan 2011 | Completed | CBD
29 Jan 2011 | Completed | JHI
29 Jan 2011 | Completed | KOP
29 Jan 2011 | Completed | YTU
29 Jan 2011 | Completed with warnings| IYR
29 Jan 2011 | Completed | ERT
29 Jan 2011 | Completed | WEQ
22 Jan 2011 | Completed | ASR
22 Jan 2011 | Completed | QWY
22 Jan 2011 | Completed | CXZ
23 Jan 2011 | Completed | FNB
23 Jan 2011 | Completed | VXR
23 Jan 2011 | Completed | CDS
30 Jan 2011 | Completed with warnings| XPO
30 Jan 2011 | Completed | JKI
30 Jan 2011 | Completed | FDP
30 Jan 2011 | Completed | MSR
30 Jan 2011 | Completed | ETP

Now if we look into the file we have jobs which did not run over the weekend like below jobs

21 jan 2011 | Completed | UVW
21 jan 2011 | Completed | XYZ
21 jan 2011 | Completed | MVU
21 jan 2011 | Completed | QPO

22 Jan 2011 | Completed | ASR
22 Jan 2011 | Completed | QWY
22 Jan 2011 | Completed | CXZ

23 Jan 2011 | Completed | FNB
23 Jan 2011 | Completed | VXR
23 Jan 2011 | Completed | CDS

The script which is stroes the information of jobs shows the status of last successfull run and that script doesnot stores the information of failed jobs and only display the last run status like above i have defined for 21 jan, 22 jan and 23 jan

What i want to do

Because the file stores the date as a string when i will read file through perl

first i will split a string on | base
After that the first array string [0] which is date would be converted in a date
After converting the string in to a date i want to compare it with the current system or the date between friday saturday and sunday it the string[0] have the date equals to the date of friday , saturday and sunday's date then its mean the job was successfully completed and also i want to add 1 in the total of successfull jobs
And If the string[0] date is not equal to the date between friday, saturday and sunday's date then its mean the job was not run then in such cases i want to add 1 in the failed jobs and also i want to get the names of failed jobs so that i will be notified that these numbers of jobs were failed

The output would be look like

Total Successfull jobs = 90
Total failed jobs = 10
Name of jobs are

UVW
XYZ
MVU
QPO
ASR
QWY
CXZ
FNB
VXR
CDS

so far i have written the following code please help me out to finish this

#!/usr/local/bin/perl

use strict;
use warnings;
use config;
use HTTP::Date  qw(parse_date);
#use Date::Manip::Date;


my @values;
my $values;

&ReadFile();
sub ReadFile
{

#       my $strFile = (@_)[0];
        my $strFile = 'C://report.txt';
        my $i =0;
        #print "$strFile\n";
        open (CONFIGFILE, "$strFile") || die ("Could not open file!");
        while (<CONFIGFILE>)
        {
                chomp;
                next if /^\s*$/;
                
                my $strLine = $_;
                my $strLine2 = trim($strLine);
                my @values = split (' \|\ ',$strLine2) ;
		print parse_date($values[0]); print"\n";
	
		foreach my $val (@values) {
   		 print "$val\n";
  }
print "\n";

                
        }



        close (CONFIGFILE);
}

# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
        my $string = shift;
        $string =~ s/^\s+//;
        $string =~ s/\s+$//;
        return $string;
}

Thanks in advance

Edited 5 Years Ago by realoneomer: n/a

#!/usr/bin/perl
use strict;
use warnings;

use constant {SECONDS_IN_DAY => 86400};
use HTTP::Date qw(time2str str2time parse_date);

my $today_YMD = epoch2YMD(time);
my $today_epoch = str2time($today_YMD);
my $today_minus_3_epoch = $today_epoch - 3 * SECONDS_IN_DAY;
my $today_minus_3_YMD = epoch2YMD($today_minus_3_epoch);
my (@completed, @not_completed);

my $filename = 'omar.txt';
open my $fh, '<', $filename or die "Failed to open $filename: $!";

while (<$fh>){
    chomp;
    my @rec = split(/\|/);
    my $jobdt = epoch2YMD(str2time($rec[0]));
    if ($jobdt lt $today_minus_3_YMD){
        push @not_completed, \@rec;
    }
    else{
        push @completed, \@rec;
    }
}
#use Data::Dumper;
#print Dumper(\@not_completed);
print "Total successful jobs = " . scalar(@completed) . "\n";
print "Total failed jobs = " . scalar(@not_completed) . "\n";
print "Name of Failed jobs are: ";

my @arr;
foreach (@not_completed){
    push @arr, @{$_}[2];
}
print join(', ', @arr);

sub epoch2YMD{
    #Converts epoch datetime number (seconds since beginning of epoch)
    #Drops time portion and returns date-only string as YYYY-MM-DD
    my $epoch = shift;
    die "Usage is epoch2YMD(epoch)" unless ($epoch);
    my $today_YMD = substr(parse_date( localtime($epoch)),0,10);
}

Output today (Tue, Feb 1) is:

Total successful jobs = 11
Total failed jobs = 18
Name of Failed jobs are:  ABC,  DEF,  GHI,  JKL,  LMN,  OPQ,  RST,  UVW,  XYZ,  MVU,  QPO,  CBD,  ASR,  QWY,  CXZ,  FNB,  VXR,  CDS

Edited 5 Years Ago by d5e5: n/a

This question has already been answered. Start a new discussion instead.