Need to get the dates of all months of the year

I need to sort the data on a weekly basis, and all I have is the dates in the log file. Therefore, to sort the data for the week, I would like to create a list with the dates of all Mondays for a certain year. I tried to do something, and the only idea I currently have is to use ncal over the years and months as the argument for the cycle for all months and extracting all Mondays. Is there a more efficient way?

+4
source share
5 answers

To get all Mondays by getting all dates and filtering on Mondays:

for i in `seq 0 365` do date -d "+$i day" done | grep Mon 

Of course, you can also take Monday and continue to increase by 7 days.

+3
source

hope you mean. Below you can change the format of the output date formats.

A date command can be used for this.

if not ncal is more / less effective.

I know that you went for binning now, but here is the more readable v.

 $ cat /tmp/1.sh #!/bin/bash test -z "$year" && { echo "I expect you to set \$year environment variable" echo "In return I will display you the Mondays of this year" exit 1 } # change me if you would like the date format to be different # man date would tell you all the combinations you can use here DATE_FORMAT="+%Y-%m-%d" # change me if you change the date format above. I need to be # able to extract the year from the date I'm shoing you GET_YEAR="s/-.*//" # this value is a week, in milliseconds. Changing it would change # what I'm doing. WEEK_INC=604800 # Use another 3-digit week day name here, to see dates for other week days DAY_OF_WEEK=Mon # stage 1, let find us the first day of the week in this year d=1 # is it DAY_OF_WEEK yet? while test "$(date -d ${year}-1-${d} +%a)" != "$DAY_OF_WEEK"; do # no, so let look at the next day d=$((d+1)); done; # let ask for the milliseconds for that DAY_OF_WEEK that I found above umon=$(date -d ${year}-1-${d} +%s) # let loop until we break from inside while true; do # ndate is the date that we testing right now ndate=$(date -d @$umon "$DATE_FORMAT"); # let extract year ny=$(echo $ndate|sed "$GET_YEAR"); # did we go over this year? If yes, then break out test $ny -ne $year && { break; } # move on to next week umon=$((umon+WEEK_INC)) # display the date so far echo "$ndate" done 
+2
source

No need to sort through all 365 or 366 days a year. Below is executed date no more than 71 times.

 #!/bin/bash y=2011 for d in {0..6} do if (( $(date -d "$y-1-1 + $d day" '+%u') == 1)) # +%w: Mon == 1 also then break fi done for ((w = d; w <= $(date -d "$y-12-31" '+%j'); w += 7)) do date -d "$y-1-1 + $w day" '+%Y-%m-%d' done 

Output:

 2011-01-03 2011-01-10 2011-01-17 2011-01-24 2011-01-31 2011-02-07 2011-02-14 2011-02-21 2011-02-28 2011-03-07 . . . 2011-11-28 2011-12-05 2011-12-12 2011-12-19 2011-12-26 
+2
source

Another option I came up with based on the above answers. Now you can specify the start and end date.

 #!/bin/bash datestart=20110101 dateend=20111231 for tmpd in {0..6} do date -d "$datestart $tmpd day" | grep -q Mon if [ $? = 0 ]; then break fi done for ((tmpw = $tmpd; $(date -d "$datestart $tmpw day" +%s) <= $(date -d "$dateend" +%s); tmpw += 7)) do echo `date -d "$datestart $tmpw day" +%d-%b-%Y` done 
+2
source

You can get the current week number using date . Maybe you can sort about this:

 $ date +%W -d '2011-02-18' 07 
0
source

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


All Articles