Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

learn linux and unix commands - unix shell scripting

Compare two dates using Shell Programming

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-26-2007   -   Original Discussion by dave_nithis
dave_nithis's Unix or Linux Image
dave_nithis dave_nithis is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 8 September 2009, 3:07 AM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Data Compare two dates using Shell Programming

Hi all,

a=2007-05-10 (YYYY-DD-MM Format)
b=2007-06-10

These are the two given dates and I need to compare.
(First It should split the dates into YYYY,dd,mm)

The script should first compare years(2007 here).If both are same or if "a" is lesser than "b"(ie.suppose year in "a" is 2006),it should print correct.If "a"(ie. suppose year in a is 2008) is greater than b,then it should print error.Similarly for the dates(05 and 06).

No need to check for the months(10).

Regards,
Dave Nithis.
Sponsored Links
    #2  
Old Unix and Linux 10-26-2007   -   Original Discussion by dave_nithis
Yogesh Sawant's Unix or Linux Image
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 20 December 2017, 6:02 AM EST
Location: Rossem, Tazenda
Posts: 1,213
Thanks: 7
Thanked 18 Times in 17 Posts
this might be useful
Sponsored Links
    #3  
Old Unix and Linux 10-26-2007   -   Original Discussion by dave_nithis
jim mcnamara's Unix or Linux Image
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 10 January 2018, 8:52 PM EST
Location: NM
Posts: 11,278
Thanks: 581
Thanked 1,125 Times in 1,036 Posts
convert the dates to a long integer (ksh) and then subtract


Code:
#!/bin/ksh
 a="2007-10-27"
 b="2006-03-22"
if [[ $((  $(echo $a | tr -d '-')   -  $(echo $b | tr -d '-')  )) -ge 0 ]] ; then
   print "correct"
else
   print "failure"    
fi

    #4  
Old Unix and Linux 10-26-2007   -   Original Discussion by dave_nithis
grulos's Unix or Linux Image
grulos grulos is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 17 February 2008, 4:35 AM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
in bash you could do
(( ${a:0:4} <= ${b:0:4} )) && echo true || echo false
or if you want to check year AND day
(( ${a:0:4} <= ${b:0:4} && ${a:5:2} <= ${b:5:2} )) && echo true|| echo false
Sponsored Links
    #5  
Old Unix and Linux 01-04-2008   -   Original Discussion by dave_nithis
dave_nithis's Unix or Linux Image
dave_nithis dave_nithis is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 8 September 2009, 3:07 AM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jim mcnamara View Post
convert the dates to a long integer (ksh) and then subtract


Code:
#!/bin/ksh
 a="2007-10-27"
 b="2006-03-22"
if [[ $((  $(echo $a | tr -d '-')   -  $(echo $b | tr -d '-')  )) -ge 0 ]] ; then
   print "correct"
else
   print "failure"    
fi
Can you explain the if part?
Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare Dates. Nagaraja Akkiva Shell Programming and Scripting 4 08-07-2011 02:08 AM
compare dates using shell sript LavanyaP Shell Programming and Scripting 2 05-13-2011 06:15 PM
compare between the two dates balaji23_d Shell Programming and Scripting 1 01-08-2009 07:47 AM
Compare one file with 7 files in shell programming ravi214u Shell Programming and Scripting 4 07-07-2008 11:08 AM
How to compare the dates in shell script vaji Shell Programming and Scripting 9 02-28-2007 12:34 AM



All times are GMT -4. The time now is 03:48 AM.