Problem with Date Comparision

Hi,

I have a file which has the date in the last line in the example pasted along with the rates of the countries.

-- I want to compare the date in the last line of the file mentioned in the example below with the system date from Monday to Friday.
-- If system date is equal to the date in the Last line then ignore else send a mail notification
--For Saturday it should compare the sysdate-1 with the date in the last line from the file

--For Sunday it should compare the sysdate-2 with the date in the last line from the file.

Can any one help me a shell script in this regard?


Example: Contents of the file

AUSTRALIA|AUD|27.4397|27.5252|28.0534
NEW ZEALAND|NZD|22.1046|22.1768|22.6529
PAKISTAN *|PKR|0.4014|0.4032|0.4279
CANADA|CAD|30.5810|30.6682|31.1338
SWEDEN|SEK|4.4355|4.4524|4.5209
DENMARK|DKK|6.4231|6.4423|6.5412
NORWAY|NOK|5.3448|5.3617|5.4429
CHINA|CNY|4.9459|4.9570|5.0135

  • Averaged from few commercial banks
    Released on 24 July 2009

Here is one way of doing what you what to do using ksh93

#!/bin/ksh93

redirect 3< file 
3<#'Released on*'
read -u3 junk1 junk2 date
redirect 3<&- 

if (( $(printf "%(%s)T" "$date") == $(printf "%(%s)T") ))
then
    print "date in file is todays date"
else
    print "date in file is earlier than today"
fi

Here's one way to do it in Perl:

$ 
$ cat data.txt
AUSTRALIA|AUD|27.4397|27.5252|28.0534
NEW ZEALAND|NZD|22.1046|22.1768|22.6529
PAKISTAN *|PKR|0.4014|0.4032|0.4279
CANADA|CAD|30.5810|30.6682|31.1338
SWEDEN|SEK|4.4355|4.4524|4.5209
DENMARK|DKK|6.4231|6.4423|6.5412
NORWAY|NOK|5.3448|5.3617|5.4429
CHINA|CNY|4.9459|4.9570|5.0135
* Averaged from few commercial banks
Released on 24 July 2009
$ 
$ cat cmp_date.pl
#!/usr/bin/perl -w
use Date::Calc qw(:all);
%mth = qw(1 Jan 2 Feb 3 Mar 4 Apr 5 May 6 Jun
          7 Jul 8 Aug 9 Sep 10 Oct 11 Nov 12 Dec);

# get date from file
open( F1, "data.txt" ) or die "Can't open data.txt: $!";
while (<F1>) { chomp( $line = $_ ); }
close(F1) or die "Can't close data.txt: $!";
$line =~ s/Released on //;
@fd = Decode_Date_EU($line);  # decode file date and assign to array fd
##
print "Today is:  ", Date_to_Text( Today() ), "\n";

# if day of week > 5 (sat and sun) then change ref date
if ( Day_of_Week( Today() ) > 5 ) {
    @rd = Add_Delta_Days( Today(), 5 - Day_of_Week( Today() ) );
                         # today-1 if sat; today-2 if sun
} else {
    @rd = Today();       # today if mon-fri
}
print "File date: $fd[2]-$fd[1]-$fd[0]\n";
print "Ref  date: $rd[2]-$rd[1]-$rd[0]\n";
cmp_and_print( @fd, @rd );

# function to compare and print
sub cmp_and_print {
    ( $y1, $m1, $d1, $y2, $m2, $d2 ) = @_;
    $cmp = ( Date_to_Days( $y1, $m1, $d1 ) <=> Date_to_Days( $y2, $m2, $d2 ) );
    print "$d1-$mth{$m1}-$y1 is ",
          $cmp == 1 ? "greater than" : $cmp == 0 ? "equal to" : "less than",
          " $d2-$mth{$m2}-$y2\n";
}

$ 
$ perl cmp_date.pl
Today is:  Mon 27-Jul-2009
File date: 24-7-2009
Ref  date: 27-7-2009
24-Jul-2009 is less than 27-Jul-2009
$ 
$ 

tyler_durden