3

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
2
  • 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. Aug 28, 2013 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. Aug 28, 2013 at 20:51

3 Answers 3

9
#!/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
2
  • 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, 2013 at 12:55
  • It's just a lexical (string) comparison: gnu.org/software/bash/manual/… Aug 29, 2013 at 17:09
3

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.

6
  • 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
    – AiB
    Aug 28, 2013 at 22:03
  • @Abraham - double check it, I just copy/pasted it into a terminal and it ran fine.
    – slm
    Aug 28, 2013 at 22:21
  • @Stephane -I don't know, it is still not working for me. The same error massage.
    – AiB
    Aug 29, 2013 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)}'
    – AiB
    Aug 29, 2013 at 0:34
  • @Abraham, as I said, you need GNU awk. That's GNU specific, it won't work with any other awk. Aug 29, 2013 at 10:02
2

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))

1
  • 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!
    – AiB
    Aug 28, 2013 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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