How to generate a range of nonweekend dates using tools available in bash? How to generate a range of nonweekend dates using tools available in bash? shell shell

How to generate a range of nonweekend dates using tools available in bash?


try this 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 this for NOV-2011 to DEC-2013

for ym in {2011' '{11,12},{2012..2013}' '{1..12}}; do  read y m <<<$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 end of this month "hard"

for ym in {2011' '{11,12},2012' '{1..7}};do  read y m <<<$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


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 the weekends
  • xargs feeds day parameters for the date command.


A 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 uses a nice property of mktime that automatically find the correct date when you increment one of the date components (e.g. "2012-02-33" becomes "2012-03-04").