Sponsored Content
Top Forums Shell Programming and Scripting Problem with Date Comparision Post 302338405 by durden_tyler on Monday 27th of July 2009 10:58:12 PM
Old 07-27-2009
Quote:
Originally Posted by Raji_gadam
...
-- 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.
...
Here's one way to do it in Perl:

Code:
$ 
$ 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
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

date comparision

Hello, I am trying to compare two dates which are in variables and i am not getting the correct result as expected. $first_date = '07/19/2007' $second_date='07/20/2007' Can you please suggest how to i check whether they are equal or not. if ($first_date -eq $second_date) condition is not... (3 Replies)
Discussion started by: kotasateesh
3 Replies

2. Solaris

date comparision

I am wriitng a shell script to compare a date in `date +%Y%m%d%H%M` to the output of `cat /var/adm/messages.0 | grep Temp |grep "CPU Fans" | cut -f1-4,19 -d' '` which looks like Dec 2 11:30:57 79 Dec 2 11:37:58 79 Dec 2 11:44:58 79 Dec 2 11:50:59 79 Dec 2 11:58:00 79 Dec 2... (1 Reply)
Discussion started by: Hitesh Shah
1 Replies

3. Shell Programming and Scripting

Date Comparision in the File

Hi All, I have thefollowing files in the directory inbox/sat ras.sat.trn.20090103.001902.00004358 ras.sat.trn.20090612.001903.00005339 ras.sat.trn.20090723.001902.00004358 The above file contains the date of the file creation. We just need to write a ksh shell script to check the... (5 Replies)
Discussion started by: satheesh_color
5 Replies

4. Shell Programming and Scripting

problem in date comparision

#!/bin/ksh var="2009-08-31 12:39:05 UTC" ddate=`echo $var|cut -d' ' -f1` y1=`echo $ddate|cut -d'-' -f1` m1=`echo $ddate|cut -d'-' -f2` d1=`echo $ddate|cut -d'-' -f3` filedate=$y1$m1$d1 currdate="20070814" if ]; then echo "$LINE -> $filedate LOWER THAN $currdate" ... (0 Replies)
Discussion started by: HemaV
0 Replies

5. Programming

Problem with date comparision in Pro*C

Hi, I have written the following Pro*C program to get the difference between 2 dates.But when I am printing the value of the date difference,it is always showing 0. #include<stdio.h> #include<sqlca.h> #include<oraca.h> void main() { EXEC SQL BEGIN DECLARE SECTION; int diff; ... (4 Replies)
Discussion started by: sureshbabuc
4 Replies

6. Shell Programming and Scripting

Date - String comparision

Hi, I am having difficulty to compare a string in a file against a date from a a table and print the latest date. Below are the values. String in File : 2009-12-02 00:37:51 Value Table : 2010-01-10-02.00.49.294758 I have to compare both the values ( Ignore the Microsecond in the table... (5 Replies)
Discussion started by: sam_78_nyc
5 Replies

7. Shell Programming and Scripting

Date Manipulation and Comparision

Hi All, I want to generate quarter dates using awk or anything, by giving a dates as input for example : start_date=2010-01-01 end_date=2010-05-31 output should be: start_date end_date qtr 2010-01-01 2010-03-31 1 2010-04-01 2010-05-31 2 Example 2:... (1 Reply)
Discussion started by: sol_nov
1 Replies

8. Shell Programming and Scripting

problem in variable comparision

Hi All, I need to compare the string variable with input value which have more than one word which also should be case insensitive. #!/bin/sh echo "please choose your choice: " read choice if ` ] then echo "correct" else echo "wrong" fi but i am getting the output for first word... (3 Replies)
Discussion started by: kalai
3 Replies

9. Shell Programming and Scripting

Date comparision in Unix

Hi All, I would need your help to compare dates in my script. Say if I have the dates in a file I need to comapre these dates with yesterday's date and the dates which are older than yesterday needs to be displayed. Example: 03/22/2012 03/24/2012 03/20/2012 03/21/2012 03/12/2012... (1 Reply)
Discussion started by: pdreddy34
1 Replies

10. Shell Programming and Scripting

Date command comparision with a different format

I have a script called " passwd_status " which gives the passwd-s status of my servers. the script output is like below password status for A: abc ks 10/05/115 1 30 ps password status for B: abc ks 09/25/115 1 30 ps password status for C: abc ks 10/10/115 1 30 ps Now , i want to... (1 Reply)
Discussion started by: charanarjun
1 Replies
DDATE(1)						     Emperor Norton Utilities							  DDATE(1)

NAME
ddate - converts Gregorian dates to Discordian dates SYNOPSIS
ddate [+format] [date] DESCRIPTION
ddate prints the date in Discordian date format. If called with no arguments, ddate will get the current system date, convert this to the Discordian date format and print this on the stan- dard output. Alternatively, a Gregorian date may be specified on the command line, in the form of a numerical day, month and year. If a format string is specified, the Discordian date will be printed in a format specified by the string. This mechanism works similarly to the format string mechanism of date(1), only almost completely differently. The fields are: %A Full name of the day of the week (i.e., Sweetmorn) %a Abbreviated name of the day of the week (i.e., SM) %B Full name of the season (i.e., Chaos) %b Abbreviated name of the season (i.e., Chs) %d Ordinal number of day in season (i.e., 23) %e Cardinal number of day in season (i.e., 23rd) %H Name of current Holyday, if any %N Magic code to prevent rest of format from being printed unless today is a Holyday. %n Newline %t Tab %X Number of days remaining until X-Day. (Not valid if the SubGenius options are not compiled in.) %{ %} Used to enclose the part of the string which is to be replaced with the words "St. Tib's Day" if the current day is St. Tib's Day. %. Try it and see. EXAMPLES
% ddate Sweetmorn, Bureaucracy 42, 3161 YOLD % ddate +'Today is %{%A, the %e of %B%}, %Y. %N%nCelebrate %H' Today is Sweetmorn, the 42nd of Bureaucracy, 3161. % ddate +"It's %{%A, the %e of %B%}, %Y. %N%nCelebrate %H" 26 9 1995 It's Prickle-Prickle, the 50th of Bureaucracy, 3161. Celebrate Bureflux % ddate +"Today's %{%A, the %e of %B%}, %Y. %N%nCelebrate %H" 29 2 1996 Today's St. Tib's Day, 3162. BUGS
ddate(1) will produce undefined behavior if asked to produce the date for St. Tib's day and its format string does not contain the St. Tib's Day delimiters %{ and %}. NOTE
After `X-Day' passed without incident, the Church of the SubGenius declared that it had got the year upside down - X-Day is actually in 8661 AD rather than 1998 AD. Thus, the True X-Day is Cfn 40, 9827. AUTHOR
Original program by Druel the Chaotic aka Jeremy Johnson (mpython@gnu.ai.mit.edu) Major rewrite by Lee H:. O:. Smith, KYTP, aka Andrew Bulhak (acb@dev.null.org) Five tons of flax. DISTRIBUTION POLICY
Public domain. All rites reversed. SEE ALSO
date(1), http://www.subgenius.com/ Malaclypse the Younger, Principia Discordia, Or How I Found Goddess And What I Did To Her When I Found Her AVAILABILITY
The ddate command is part of the util-linux package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux/. 59 Bureaucracy 3161 DDATE(1)
All times are GMT -4. The time now is 03:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy