How to create a date range without waiting with the tools available in bash?

I want to generate a list of files in which the name consists of ${filename}.${date} , for example file.20111101 , file.20120703 , from November 1, 2011 until today, and it should exclude weekends.

Thanks.

+2
source share
4 answers

try it for 2011

 for y in 2011; do for m in {1..12}; do for d in `cal -m $m $y|tail -n +3|cut -c 1-15`; do printf "file.%04d%02d%02d\n" $y $m $d; done; done; done 

or is it for NOV-2011 to DEC-2013

 for ym in {2011' '{11,12},{2012..2013}' '{1..12}}; do read ym <<<$ym; for d in `cal -m $m $y|tail -n +3|cut -c 1-15`; do printf "file.%04d%02d%02d\n" $y $m $d; done; done 

or until the end of this month "hard"

 for ym in {2011' '{11,12},2012' '{1..7}};do read ym <<<$ym; for d in `cal -m $m $y|tail -n +3|cut -c 1-15`;do printf "file.%04d%02d%02d\n" $y $m $d; done; done 
+5
source

This might work for you (GNU date, sed and Bash):

 seq 0 $((($(date +%s)-$(date -d 20111101 +%s))/86400)) | sed '5~7d;6~7d' | xargs -l1 -I '{}' date -d '+{} days 20111101' +file.%Y%m%d 

Explanation:

  • seq provides a sequence of days from 20111101 to today
  • sed filters out weekends
  • xargs sets the day for the date command.
+2
source

Solution with gawk:

 gawk -v START_DATE="2011 11 01 0 0 0" -v FILE="file" ' BEGIN { END_DATE = strftime( "%Y %m %d 0 0 0", systime() ) while ( START_DATE <= END_DATE ) { split( START_DATE, DATE ) if ( strftime( "%u", mktime( START_DATE ) ) < 6 ) print FILE "." DATE[ 1 ] DATE[ 2 ] DATE[ 3 ] START_DATE = strftime( "%Y %m %d 0 0 0", mktime( DATE[ 1 ] " " DATE[ 2 ] " " ( DATE[ 3 ]+1 ) " 0 0 0" ) ) } } ' 

This property uses the mktime property, which automatically finds the correct date when one of the date components is added (for example, "2012-02-33" becomes "2012-03-04").

0
source

Another option is to use dateseq from dateutils ( http://www.fresse.org/dateutils/#dateseq ).

 $ dateseq 2017-03-25 $(date +%F) --skip sat,sun -ffile.%Y%m%d file.20170327 file.20170328 file.20170329 file.20170330 file.20170331 file.20170403 file.20170404 file.20170405 file.20170406 file.20170407 
0
source

Source: https://habr.com/ru/post/1201971/


All Articles