Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I want to print dates between 2 different dates (i.e. start date and two dates)

If start date is 2013-09-05 and end date is 2013-09-10, then I want result as

2013-09-05
2013-09-06
2013-09-07
2013-09-08
2013-09-09 
2013-09-10 

..... etc

During month end and month start, it should print correct dates.

share|improve this question

marked as duplicate by Stéphane Chazelas, Mat, jasonwryan, slm, rahmu Sep 29 '13 at 20:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

The easiest way is to use the date command. In the UNIX world, dates are measured in seconds since the epoch. If you convert your dates to seconds, you can use date to print out the ones you are interested in:

END=$(date -d "2013-09-10" +%s);
DATE=$(date -d "2013-09-05" +%s); 
while [[ "$DATE" -le "$END" ]]; do date -d "@$DATE" +%F; let DATE+=86400; done

Copy/pasting that into your terminal should return

2013-09-05
2013-09-06
2013-09-07
2013-09-08
2013-09-09
2013-09-10

The relevant sections of man date:

   -d, --date=STRING
          display time described by STRING, not 'now'

   %F     full date; same as %Y-%m-%d
share|improve this answer
    
Note that in the Unix world, date doesn't take a -d option (or when it does it's not always for that). In the GNU world, it does though, but the ksh tag suggests the OP's is more likely to be in a Unix than GNU world. –  Stéphane Chazelas Sep 29 '13 at 19:03
    
@StephaneChazelas ok, thanks. Any ideas on how to do this without -d? The only thing I said about the Unix world was that dates are expressed in seconds since the epoch, I did not mean to imply that the Unix date can take -d. –  terdon Sep 29 '13 at 19:07
    
A few options there –  Stéphane Chazelas Sep 29 '13 at 19:09
    
@user48204 what operating system do you have? StephaneChazelas points out above that -d is a GNU specific flag. –  terdon Sep 30 '13 at 17:27
    
Hi, i am getting below error ; Usage: date [-u] [+"Field Descriptors"] ./bkdt.sh[13]: DATE+=86400: 0403-009 The specified number is not valid for this command. date: Not a recognized flag: d Usage: date [-u] [+"Field Descriptors"] ./bkdt.sh[13]: DATE+=86400: 0403-009 The specified number is not valid for this command. date: Not a recognized flag: d Usage: date [-u] [+"Field Descriptors"]. Kindly suggest. –  user48204 Sep 30 '13 at 17:28

Using GNU grep:

grep -A5 "2013-09-05" file

Using awk:

awk '/2013-09-05/,/2013-09-10/{print}' file

Using sed:

sed -n '/2013-09-05/,/2013-09-10/p' file

Or, reading the comment, using GNU date:

start=2013-09-05 
end=2013-09-11
   while [[ $start < $end ]] 
     do 
      printf "$start\n"; start=$(date -d "$start + 1 day" +"%Y-%m-%d") 
     done
share|improve this answer
    
Don't know that the OP has a file with all the dates in them. I got the distinct impression he's looking to be able to input a date range and have all dates in the range generated –  1_CR Sep 29 '13 at 18:33
    
@1_CR. Well if none of these would help, then I'm out of options. –  val0x00ff Sep 29 '13 at 18:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.