List all the mondays of this month

折月煮酒 提交于 2021-01-27 13:20:25

问题


I'm pretty new to bash and all the terminal in general - I've been messing around with cal and the date scripts wondering if there is anyway to list all the dates of monday of the current month . My thought process is going thru the cal command, listing out the dates and maybe cutting a column from that input. Is that possible ?


回答1:


You can do it with date command. Print 10 mondays since month ago:

for x in $(seq 0 9)
do
    date -d "$x monday 5 week ago"
done

And grep only current month. Full command: for x in $(seq 0 9); do; date -d "$x monday 5 week ago"; done | grep $(date +%b)

Output:

Mon Jun  5 00:00:00 MSK 2017
Mon Jun 12 00:00:00 MSK 2017
Mon Jun 19 00:00:00 MSK 2017
Mon Jun 26 00:00:00 MSK 2017



回答2:


Given:

$ cal
     June 2017
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

You can do:

$ cal | awk 'NF>5{print $2}'
Mo
5
12
19
26

If you want something that will support any day of cal, use the field width (gawk only this):

$ cal | gawk -v n=5 '
            BEGIN{
               FIELDWIDTHS = "3 3 3 3 3 3 3"
           }
           FNR>1{print $n}'
Th 
 1 
 8 
15 
22 
29

Or, as pointed out in comments:

$ ncal | awk '/^Mo/'
Mo     5 12 19 26



回答3:


Combination of cal,cut commands to achieve the output.

cal -h| cut -c'4,5'

Remove the highlight and cut the characters which suits in the fields of monday.

ncal | sed -n '/^Mo/p'

The output as below:

Mo     5 12 19 26   


来源:https://stackoverflow.com/questions/44622980/list-all-the-mondays-of-this-month

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!