3

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

Thanks.

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
vehomzzz
  • 42,832
  • 72
  • 186
  • 216

4 Answers4

6

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
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • 2
    excuse my reformatting... but I think it improves readability and eases understanding of your nice suggestion. (Note, that for my version of `cal` (on Mac OS X) I do get a few error messages to stdout, which also cause the list of printed filenames to include multiple instances of (invalidly named) `file.20131200`, `file.20131100`,... as well as multiple copies of valid names.) – Kurt Pfeifle Jul 03 '12 at 22:58
  • are you using bash and does brace expansion work: replace inner for by echo $m $y, also look at man cal -m option, it must display monday in first else it's sunday and cut parameters should be change to -c 4-18 – Nahuel Fouilleul Jul 04 '12 at 09:17
  • Yes, I'm using bash. Brace expansion also works. Replacing inner for by `echo $m $y` I already did before your comment (and it shows the same error messages in between valid output, and also shows multiple copies of valid names). My `cal` supports `-m` according to man page. -- Yes, you are right that my `cal` shows sunday as first day. – Kurt Pfeifle Jul 04 '12 at 10:22
  • Oh, contrary to what the OSX manpage says, the `cal -m 7 2012` doesn't display a single month, but the complete year. That explains my results. ;-( – Kurt Pfeifle Jul 04 '12 at 10:54
  • Option `-1` is not supported by Mac OS X's `cal`. I couldn't find a GNU cal in MacPorts either... – Kurt Pfeifle Jul 04 '12 at 16:20
2

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.
potong
  • 55,640
  • 6
  • 51
  • 83
  • using date, this may work too : for d in {20111101..$(date +%Y%m%d)};do [[ "$d" =~ ^\d\d\d\d[0-1]\d[0-3]\d$ ]] && [[ "$(date -d $d +%a 2>/dev/null)" =~ ^[MTWF]..$ ]] && date -d $d +%Y%m%d; done – Nahuel Fouilleul Jul 04 '12 at 16:13
2

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
nisetama
  • 7,764
  • 1
  • 34
  • 21
0

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