Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate date range for random data on bash

I need to generate strings with all days in a year

ex:

MIN_DATE=01.01.2012

MAX_DATE=31.12.2012

for date in {1...366..1}
 do
 echo ...
done
like image 701
Dmitry Dubovitsky Avatar asked Aug 24 '12 10:08

Dmitry Dubovitsky


3 Answers

for d in {0..365}; do date -d "2012-01-01 + $d days" +'%d.%m.%Y'; done
like image 164
Lev Levitsky Avatar answered Sep 29 '22 13:09

Lev Levitsky


Not a pure bash solution, but my dateutils can help:

dseq 01.01.2012 31.12.2012 -f %d.%m.%Y -i %d.%m.%Y
=>
  01.01.2012
  02.01.2012
  ...
  31.12.2012

Output format can be configured with -f and input format with -i.

like image 33
hroptatyr Avatar answered Sep 29 '22 14:09

hroptatyr


Using an ISO 8601 date format (year-month-day), you can compare dates lexicographically. It's a little messier than I'd like, since bash doesn't have a "<=" operator for strings.

year=2011
d="$year-01-01"
last="$(($year+1))-01-01"
while [[ $d < $last ]]; do
    echo $d
    d=$(date +%F --date "$d + 1 day")
done
like image 25
chepner Avatar answered Sep 29 '22 14:09

chepner