|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
This should be a piece of cake if you know perl...
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Hi Shamrock,
Our environment doesn't support PERL. ![]() Is there a way we can accomplish this in LINUX coding Thanks Freddie |
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
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.
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|