I'm trying to pick off the first Monday, second Tuesday, second Saturday, etc. out of the cal
function and piping awk
commands. For this month, I can get the first Tuesday correctly with the command below the calendar:
December 2018
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 31
>cal | awk 'NF == 7 && NR > 2 {print $3; exit}'
>4
But this doesn't always work, see the test for March 2016 below:
March 2016
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 31
>cal 3 2016 | awk 'NF == 7 && NR > 2 {print $3; exit}'
>8
Why does it work for the top example but not the bottom? I would think the bottom example should look at the third line and find the correct result of 1
. Is there a fool-proof way to snag these dates using awk
-- maybe checking if the column is empty and moving to the next line?
cal
? Do you have GNUdate
?NF==7
part, but removing that gets hairy if Sunday and Monday are missing and the first day of the month is Tuesday. I do havedate
.substr
:cal | awk 'NR>1 && (d=substr($0,2*3,3)+0){print d; exit}'