Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-19-2012
Registered User
 
Join Date: Dec 2011
Posts: 69
Thanks: 8
Thanked 0 Times in 0 Posts
How to write the dates between 2 dates into a file

Hi All,

I am trying to print the dates that falls between 2 date variables into a file. Here is the example.

$BUS_DATE =20120616
$SUB_DATE=20120613

Output to file abc.txt should be : 20120613,20120614,120120615,20120616

Can you pls help me accomplish this in LINUX.

Thanks
Freddie
Sponsored Links
    #2  
Old 06-19-2012
Registered User
 
Join Date: Oct 2007
Location: USA
Posts: 1,299
Thanks: 11
Thanked 99 Times in 95 Posts
This should be a piece of cake if you know perl...
Sponsored Links
    #3  
Old 06-19-2012
Registered User
 
Join Date: Dec 2011
Posts: 69
Thanks: 8
Thanked 0 Times in 0 Posts
Hi Shamrock,

Our environment doesn't support PERL.

Is there a way we can accomplish this in LINUX coding

Thanks
Freddie
    #4  
Old 06-19-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Location: earth>US>UTC-5
Posts: 1,454
Thanks: 108
Thanked 498 Times in 477 Posts
If you have either GNU or AT&T AST's date command, then something like this is possible:


Code:
#!/usr/bin/env ksh

date1="20120601"
date2="20120614"

ndays=$(( ($(date -d $date2 "+%s") - $(date -d $date1 "+%s"))/ 86400 ))
fmt="%s"
for x in {0..$ndays}
do
    printf $fmt "$( date -d "$date1 + $x day" "+%Y%m%d" )"
    fmt=",%s"
done
printf "\n"

If you want to use bash (the {n..m} construct doesn't work in my version), then use this for statement:


Code:
for x in $( seq 0 $ndays)


Last edited by agama; 06-19-2012 at 08:55 PM.. Reason: typo
Sponsored Links
    #5  
Old 06-20-2012
Registered User
 
Join Date: Oct 2007
Location: USA
Posts: 1,299
Thanks: 11
Thanked 99 Times in 95 Posts
Quote:
Originally Posted by dsfreddie View Post
Hi Shamrock,

Our environment doesn't support PERL.
You mean perl isnt installed...there is a big difference between not installed and not supported and i think there isnt a linux distro that doesnt support perl...talk to your sysadmin and [s]he can install perl for you.
Quote:
Originally Posted by dsfreddie View Post
Is there a way we can accomplish this in LINUX coding

Thanks
Freddie
What Linux distro are you running...as the date command on linux systems is very powerful unlike the one on older unix systems.
Sponsored Links
    #6  
Old 06-20-2012
fpmurphy's Avatar
who?
 
Join Date: Dec 2003
Location: /dev/ph
Posts: 4,427
Thanks: 47
Thanked 356 Times in 330 Posts
Using ksh93

Code:
date1="20120601"
date2="20120614"

u1=$(printf "%(%s)T" "${date1:0:4}-${date1:4:2}-${date1:6:2}")
u2=$(printf "%(%s)T" "${date2:0:4}-${date2:4:2}-${date2:6:2}")

fmt=""
while (( u1 < u2 ))
do
    printf "%s%(%Y%m%d)T" "$fmt" "#$u1"
    (( u1 += 3600*24 ))
    fmt=", "
done
printf "\n"

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Changing dates in a csv file BrutalBryan Shell Programming and Scripting 1 01-05-2012 01:03 PM
Replacing dates]] with (dates)]] lawstudent Emergency UNIX and Linux Support !! Help Me!! 20 01-19-2011 03:13 PM
lines between two dates in a file Reddy482 Shell Programming and Scripting 2 11-30-2010 08:13 PM
SQL: find if a set od dates falls in another set of dates vertical98 Programming 0 10-16-2010 01:31 AM
Need script to generate all the dates in DDMMYY format between 2 dates frozensmilz Shell Programming and Scripting 2 01-29-2009 05:06 AM



All times are GMT -4. The time now is 03:42 PM.