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.

I was wondering if I could generate a hourly or daily time step date in a column starting from some year in the past to some day in the near past or today. to be more clear, I want to create one column of data from 2000-10-10 100 to 2012-12-31 2400. The output file will look like

Date 

2000-01-01 100
2000-01-01 200
2000-01-01 300
.
.
.
.
.
2012-12-31 2400
share|improve this question
1  
Without knowing the flow of your data I'm guessing you want something like echo "$(date +%F) 100 2000" >> somefile Please explain your question so people could answer the question. –  val0x00ff Aug 28 '13 at 19:29
    
Note that the second after 2012-12-31 235959 is generally expressed as 2013-01-01 000000, not 2012-12-31 240000. –  Stephane Chazelas Aug 28 '13 at 20:51
add comment

3 Answers

#!/bin/bash

day=2000-01-01
end=2012-12-31

echo Date > output.file

until [[ $day > $end ]]; do
    printf "$day %d\n" $(seq 100 100 2400)
    day=$(date -d "$day + 1 day" +"%Y-%m-%d")
done >> output.file
share|improve this answer
    
Thank you very much!! it works perfect!! Awesome –  Abraham Aug 28 '13 at 20:18
    
How is that comparison working? Is it just a byte-by-byte character comparison or does bash actually parse and interpret the date strings? –  terdon Aug 29 '13 at 12:55
    
It's just a lexical (string) comparison: gnu.org/software/bash/manual/… –  glenn jackman Aug 29 '13 at 17:09
add comment

With ksh93:

d1=$(printf '%(%s)T\n' "2000-01-01 01:00:00")
d2=$(printf '%(%s)T\n' "2013-01-01 00:00:00")
for ((d=d1;d<d2;d+=3600)); do
  printf '%(%F %-H%M)T\n' "#$d"
done

With zsh:

zmodload zsh/datetime
strftime -rs d1 '%Y%m%d%H%M' 201201010100
strftime -rs d2 '%Y%m%d%H%M' 201301010000
for ((d=d1;d<d2;d+=3600)) strftime '%F %-H%M' $d

With perl:

perl -MPOSIX -le '
  $d1=mktime 0,0,1,1,0,100;$d2=mktime 0,0,0,1,0,113;
  for ($d=$d1; $d<$d2; $d+=3600) {
    print strftime "%F %-H%M", localtime $d}'

With GNU awk:

awk 'BEGIN {d1=mktime("2000 01 01 01 00 00")
            d2=mktime("2013 01 01 00 00 00")
            for (d=d1;d<d2;d+=3600)
              print strftime("%F %-H%M",d)}'

Note that with all the above around DST changing time some hours will be skipped or output twice if in a timezone with DST as a clock would.

We're counting from 000 to 2300 instead of 100 to 2400.

share|improve this answer
    
Thank you for your answer, it has some error while running. Here is the error awk: line 5: function strftime never defined awk: line 5: function mktime never defined awk: line 5: function mktime never defined –  Abraham Aug 28 '13 at 22:03
    
@Abraham - double check it, I just copy/pasted it into a terminal and it ran fine. –  slm Aug 28 '13 at 22:21
    
@Stephane -I don't know, it is still not working for me. The same error massage. –  Abraham Aug 29 '13 at 0:34
    
#!/bin/bash awk 'BEGIN {d1=mktime("2000 01 01 01 00 00") d2=mktime("2013 01 01 00 00 00") for (d=d1;d<d2;d+=3600) print strftime("%F %-H%M",d)}' –  Abraham Aug 29 '13 at 0:34
    
@Abraham, as I said, you need GNU awk. That's GNU specific, it won't work with any other awk. –  Stephane Chazelas Aug 29 '13 at 10:02
show 1 more comment

Since the question has gotten more understandable, the following little script could do something similar.

#!/bin/bash

i=1
c=100

while ((c>i))
    do
    echo "$(date +%F)" $c >> thisfile
    sleep 5
    echo "$(date +%F)" $((c+=100))
    done

I hope it's self explanatory

I've used a sleep 5 but to get exactly this line each hour generated you can use something like sleep $((60*60))

share|improve this answer
    
first I want to thank you. I run the script and have seen the result. The problem was, it didn't go to the next day after 24 hours, rather in just starts from today at 100 and continue to nX100 for the same day. What I want is generate a date data file, hourly time step, the starting from January 2000 at 100 hour, to December 2012 at 2400 hour. Thank you! –  Abraham Aug 28 '13 at 20:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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