How to get first sunday of the month?

Hi,

Please can someone help me in getting first sunday date of a month.

i_year=`date +%Y`
ny_first_sun_nov=`cal  10 $i_year | sed  '/^$/d' |head -3 |tail -1| rev | cut -c1`

This works good if the first sunday has a value but not if it is blank and first sunday falls on second week.

cal -m where I can make Monday as first day of the week so that Sunday always have a value does not work on Solaris.

Please can someone help me to get this corrected?

Thanks

I noticed that you opened several threads lately regarding date arithmetic and you didn't apply any suggestions posted in your previous threads!

Before going forward I suggest you to refer this thread in FAQ section.

Thanks Yoda,

I will close the other threads with suggestions and comments,
All the threads correspond to different questions/ arithmetic but I should close them if they are resolved.

Thanks again

Try ..

cal 12 2012 | awk 'NF==7 && !/^Su/{print $1;exit}'
1 Like

Its not working,

cal 12 2012 | awk 'NF==7 && !/^Su/{print $1;exit}'

is giving me S
I am looking for value that is 1

That's working for me.

$ cal 1 2012 | awk 'NF==7 && !/^Su/{print $1;exit}'
1
$
$ cal 1 2012
    January 2012
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

Please use /usr/xpg4/bin/awk on solaris
Also, post the output of

cal 1 2012
cal 1 2012
   January 2012
 S  M Tu  W Th  F  S
 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 1 2012 | awk 'NF==7 && !/^Su/{print $1;exit}'

S

---------- Post updated at 10:00 PM ---------- Previous update was at 09:58 PM ----------

/usr/xpg4/bin/awk

this is also giving me S

As you can see, the output of "cal 1 2012" in your system is not the same as in that of clx.

He is using "Su" for "Sunday" - that's so he could avoid parsing the line that starts with "Su".

But you have "S" for "Sunday", so now you know the change you have to make in your script to make it work.

1 Like
cal 5 2013 | awk 'NF==7&&NR>2{print $1;exit}'
1 Like

Thanks Yoda, it worked, can we also use it to get the second sunday?
I mean to get it generalized?

Here is a generalized approach for sunday:

cal 5 2013 | /usr/xpg4/bin/awk -v n=2 'NR>2&&!/^  /{if(++c==n) {print $1;exit}}'

Change n variable value as per your requirement.

1 Like

Many Thanks to all, especially Yoda. Now I have learnt how to do this by your help

The previous expressions suffer from the format; the first week has different number of fields depending on the month.
The following tries to correct that

cal 5 2013 |
 sed 's/\(..\) /\1|/g' |
 awk -F"|" '(NF==7 || $1~/[0-9]/) && $day~/[0-9]/ && ++c==count {print $day+0}' day=1 count=2

You can put various days and counts ...

Same as I posted here:

How many threads are needed to solve one problem?

Jotne, please calm down. I noticed before that OP opened several similar threads, hence I requested him/her to refer another thread for ideas.

You can check my previous post.

But OP had some trouble with the way he/she getting cal command output, so he/she requested for additional assistance with tweaking the solutions posted.

That's all.

1 Like

I am very calm :wink:

Found this
https://bugs.launchpad.net/ubuntu/\+source/bsdmainutils/\+bug/908233

Other solution

ncal -m 5 | awk '/Su/ {print $3}'
12
1 Like

:eek:OMG 11 threads! ELEVEN THREADS!!

Time! Calendars! Didn't anyone pay attention in grade school? If day 1 of the month is not Sunday (0) according to the dow from GNU date '+%D', then it is 7-dow days away on the date ( 1 + 7 - dow )? The general solution is (( 7 - dow(1st) ) % 7 ) + 1. Ooh, the numbers go spinning around so!