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.

Date Range in UNIX

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-25-2013
adisky123 adisky123 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 December 2013, 5:57 AM EST
Posts: 58
Thanks: 5
Thanked 0 Times in 0 Posts
Date Range in UNIX

Hi Everyone How all are doing today,

Want some help from All in Unix, What I am trying to do is , A shell file should be a called with two date parameters suppose the shell file name is run.sh

run.sh <start_date> <end_date>
If end date is not given it will pick today's date. The date should be in format yyyy-mm-dd.

Now the twist is suppose I have given the dates

Code:
 run.sh 2012-01-01 2012-01-30

Inside this shell file It should pick the data range max of 6 days from start date like

at first hit the Start date and end date should be 2012-01-01 and 2012-01-07 #- 6days and print on the screen
at second hit the Start date and end date should be 2012-01-07 and 2012-01-13 #- 6days ..
.
.
.
.
at last hit the Start date and end date should be 2012-01-25 and 2012-01-30 #- 6days ..

I hope I am able to clear you
Thanks
Sponsored Links
    #2  
Old Unix and Linux 01-25-2013
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 12 June 2016, 11:03 PM EDT
Location: /dev/ph
Posts: 4,996
Thanks: 73
Thanked 475 Times in 437 Posts
Using the ksh93 shell ...

Code:
#!/bin/ksh93

nextdate=$(printf "%(%s)T" "$1")
enddate=$(printf "%(%s)T" "$2")
typeset -i DAY=24*60*60

while (( nextdate < enddate ))
do
    printf "%(%Y-%m-%d)T" "#${nextdate}"
    for ((i=1; i < 7 &&  0 == $(( (nextdate + $DAY) > enddate )); i++))
    do
       (( nextdate += $DAY ))
    done
    printf "   %(%Y-%m-%d)T\n" "#${nextdate}"
done

produces

Code:
$ ./run.sh  2012-01-01 2012-01-30
2012-01-01   2012-01-07
2012-01-07   2012-01-13
2012-01-13   2012-01-19
2012-01-19   2012-01-25
2012-01-25   2012-01-30
$

Sponsored Links
    #3  
Old Unix and Linux 02-11-2013
adisky123 adisky123 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 December 2013, 5:57 AM EST
Posts: 58
Thanks: 5
Thanked 0 Times in 0 Posts
Hello

The code fails when I am writing

Code:
./run.sh 2013-01-01 2013-05-05

---------- Post updated at 08:26 AM ---------- Previous update was at 05:13 AM ----------

It hangs on 2013-05-04
    #4  
Old Unix and Linux 02-11-2013
phunk phunk is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 28 February 2014, 3:57 PM EST
Posts: 35
Thanks: 3
Thanked 1 Time in 1 Post
while (( nextdate <= enddate ))?
Sponsored Links
    #5  
Old Unix and Linux 02-11-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 December 2016, 2:43 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 Posts
What is your OS uname and SHELL echo $SHELL?
Sponsored Links
    #6  
Old Unix and Linux 02-11-2013
adisky123 adisky123 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 December 2013, 5:57 AM EST
Posts: 58
Thanks: 5
Thanked 0 Times in 0 Posts
Linux
KSH
Sponsored Links
    #7  
Old Unix and Linux 02-11-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 December 2016, 2:43 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 Posts
If you have GNU date

Code:
#!/bin/bash

DT1=$1
DT2=$2

E_DT1=$( date -d"$DT1" +"%s" )
E_DT2=$( date -d"$DT2" +"%s" )

while [ $E_DT1 -lt $E_DT2 ]
do
        printf "%s " $( date -d@"$E_DT1" +"%Y-%m-%d" )
        DT1=$( date -d"$DT1 +6 days" +"%Y-%m-%d" )
        E_DT1=$( date -d"$DT1" +"%s" )
        [[ $E_DT1 -le $E_DT2 ]] && printf "%s\n" $( date -d@"$E_DT1" +"%Y-%m-%d" ) || printf "%s\n" $( date -d@"$E_DT2" +"%Y-%m-%d" )
done

---------- Post updated at 08:26 ---------- Previous update was at 08:25 ----------

Here is the o/p

Code:
$ ./dtrange 2013-01-01 2013-05-05
2013-01-01 2013-01-07
2013-01-07 2013-01-13
2013-01-13 2013-01-19
2013-01-19 2013-01-25
2013-01-25 2013-01-31
2013-01-31 2013-02-06
2013-02-06 2013-02-12
2013-02-12 2013-02-18
2013-02-18 2013-02-24
2013-02-24 2013-03-02
2013-03-02 2013-03-08
2013-03-08 2013-03-14
2013-03-14 2013-03-20
2013-03-20 2013-03-26
2013-03-26 2013-04-01
2013-04-01 2013-04-07
2013-04-07 2013-04-13
2013-04-13 2013-04-19
2013-04-19 2013-04-25
2013-04-25 2013-05-01
2013-05-01 2013-05-05

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
date range Niven UNIX Desktop Questions & Answers 3 02-04-2010 03:58 AM
Get date range between 2 date input tanit Shell Programming and Scripting 3 04-21-2009 12:20 PM
automated ftp script from unix -date range of files koduri0475 UNIX for Advanced & Expert Users 1 11-17-2005 02:59 PM
automated ftp script from unix -date range of files koduri0475 Programming 1 11-10-2005 10:51 AM
automated ftp script from unix -date range of files koduri0475 Shell Programming and Scripting 1 11-10-2005 10:50 AM



All times are GMT -4. The time now is 08:15 PM.