Visit Our UNIX and Linux User Community


How to write the dates between 2 dates into a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to write the dates between 2 dates into a file
# 1  
Old 06-19-2012
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
# 2  
Old 06-19-2012
This should be a piece of cake if you know perl...
# 3  
Old 06-19-2012
Hi Shamrock,

Our environment doesn't support PERL. Smilie

Is there a way we can accomplish this in LINUX coding

Thanks
Freddie
# 4  
Old 06-19-2012
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 10:55 PM.. Reason: typo
# 5  
Old 06-20-2012
Quote:
Originally Posted by dsfreddie
Hi Shamrock,

Our environment doesn't support PERL. Smilie
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
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.
# 6  
Old 06-20-2012
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"

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #425
Difficulty: Medium
C# 1.0 was first released in December 2002.
True or False?

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Grep file containing dates

How to grep a file containing dates to only last 30 days then move to another folder (7 Replies)
Discussion started by: kmarcus
7 Replies

2. Shell Programming and Scripting

Display dates between two dates

Hi All, I have 2 dates in mm/dd format. sdate=10/01 (October 01) edate=10/10 (October 10) I need the dates in between these 2 dates like below. 10/01 10/02 10/03 10/04 10/05 10/06 10/07 10/08 (1 Reply)
Discussion started by: jayadanabalan
1 Replies

3. Shell Programming and Scripting

Replacing the Dates in a file

Hello Gurus, I'm beginner in Shell scripting. I got a requirement to write a script. I have a file with below (similar) content If you can observe above content, there are many date values existed (with different dates) in a format: ddMonyyyy I have to write replace all these... (7 Replies)
Discussion started by: raghu.iv85
7 Replies

4. UNIX for Advanced & Expert Users

How to get the Missing dates between two dates in the table?

Hi Am Using Unix Ksh ... I have a Table called date select * from date ; Date 01/02/2013 06/02/2013 I need the output as Missing Date 01/02/2013 02/02/2013 03/02/2013 04/02/2013 05/02/2013 06/02/2013 (2 Replies)
Discussion started by: Venkatesh1
2 Replies

5. Shell Programming and Scripting

Generating dates between two dates

HI, i have row like this HHH100037440313438961000201001012012073110220002 N in this i have 2 dates in pos 25-32 and 33-40 , so based upon the se two dates , i need to generated records between these two values so in the above record 20100101 and 20120731 need to genearte rows like this... (4 Replies)
Discussion started by: sathishsr
4 Replies

6. Emergency UNIX and Linux Support

Replacing dates]] with (dates)]]

Hi guys, For my wiki site I need to fix 1400 pages that use the wrong date format, most pages (not all) use eg. 1988]] I need to change that to (1988)]] The date range goes back to 1400 so I guess I need to do the following ssh into my server, dump mysql database vi .sql dump search... (20 Replies)
Discussion started by: lawstudent
20 Replies

7. Programming

SQL: find if a set od dates falls in another set of dates

Don't know if it is important: Debian Linux / MySQL 5.1 I have a table: media_id int(8) group_id int(8) type_id int(8) expiration date start date cust_id int(8) num_runs int(8) preferred_time int(8) edit_date timestamp ON UPDATE CURRENT_TIMESTAMP id... (0 Replies)
Discussion started by: vertical98
0 Replies

8. UNIX for Dummies Questions & Answers

Extract dates from file name.

Hi All, I have files with names as us_Gec1_wk_01to01_2008.TXT ad_EngEnt_wk_01to10_2008.TXT br_EngMov_wk_01to10_2008.TXT Over here, I need to extract the dates and the year and store them in variables. How can I achieve the same in bash. In case of ad_EngEnt_wk_01to10_2008.TXT ... (3 Replies)
Discussion started by: Swapna173
3 Replies

9. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. Shell Programming and Scripting

Help to switch dates from a file

Hi. I need some assistance with a file who at the end i must import to EXCEL. The problem is that i have a file with this inside: Output: JOBID START TIME END TIME ELAPSED CPU ------------------------------------------------------------ AVERAGE: ... (2 Replies)
Discussion started by: osramos
2 Replies

Featured Tech Videos