# How to get first sunday of the month?

**URL:** <https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159>\
**Category:** Shell Programming and Scripting\
**Created:** [May 8, 2013, 11:12am UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159 "2013-05-08T11:12:13Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![infyanurag](https://community.unix.com/letter_avatar/infyanurag/32/5_5575768a8748004e209b776fc1b2916d.png) [@infyanurag](https://community.unix.com/u/infyanurag)\
**Post date:** [May 8, 2013, 11:12am UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/1 "2013-05-08T11:12:13Z")

</div>

Hi,

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

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [May 8, 2013, 11:33am UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/2 "2013-05-08T11:33:21Z")

</div>

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](http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html) in FAQ section.

---

<div class="post-metadata">

**Author:** ![infyanurag](https://community.unix.com/letter_avatar/infyanurag/32/5_5575768a8748004e209b776fc1b2916d.png) [@infyanurag](https://community.unix.com/u/infyanurag)\
**Post date:** [May 8, 2013, 11:37am UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/3 "2013-05-08T11:37:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![clx](https://community.unix.com/letter_avatar/clx/32/5_5575768a8748004e209b776fc1b2916d.png) [@clx](https://community.unix.com/u/clx)\
**Post date:** [May 8, 2013, 11:51am UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/4 "2013-05-08T11:51:17Z")

</div>

Try ..

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

```

---

<div class="post-metadata">

**Author:** ![infyanurag](https://community.unix.com/letter_avatar/infyanurag/32/5_5575768a8748004e209b776fc1b2916d.png) [@infyanurag](https://community.unix.com/u/infyanurag)\
**Post date:** [May 8, 2013, 12:08pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/5 "2013-05-08T12:08:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![clx](https://community.unix.com/letter_avatar/clx/32/5_5575768a8748004e209b776fc1b2916d.png) [@clx](https://community.unix.com/u/clx)\
**Post date:** [May 8, 2013, 12:16pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/6 "2013-05-08T12:16:38Z")

</div>

That's working for me.

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

```

```nohighlight
$ 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

```nohighlight
cal 1 2012

```

---

<div class="post-metadata">

**Author:** ![infyanurag](https://community.unix.com/letter_avatar/infyanurag/32/5_5575768a8748004e209b776fc1b2916d.png) [@infyanurag](https://community.unix.com/u/infyanurag)\
**Post date:** [May 8, 2013, 12:30pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/7 "2013-05-08T12:30:16Z")

</div>

```plaintext
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 ----------

> [@infyanurag](#):
>
> ```plaintext
> 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

/usr/xpg4/bin/awk

this is also giving me S

---

<div class="post-metadata">

**Author:** ![durden\_tyler](https://community.unix.com/user_avatar/community.unix.com/durden_tyler/32/1450_2.png) [@durden\_tyler](https://community.unix.com/u/durden_tyler)\
**Post date:** [May 8, 2013, 12:35pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/8 "2013-05-08T12:35:09Z")

</div>

> [@infyanurag](#):
>
> ```plaintext
> 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.

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [May 8, 2013, 12:37pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/9 "2013-05-08T12:37:48Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![infyanurag](https://community.unix.com/letter_avatar/infyanurag/32/5_5575768a8748004e209b776fc1b2916d.png) [@infyanurag](https://community.unix.com/u/infyanurag)\
**Post date:** [May 8, 2013, 1:02pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/10 "2013-05-08T13:02:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [May 8, 2013, 1:25pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/11 "2013-05-08T13:25:54Z")

</div>

Here is a generalized approach for sunday:

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![infyanurag](https://community.unix.com/letter_avatar/infyanurag/32/5_5575768a8748004e209b776fc1b2916d.png) [@infyanurag](https://community.unix.com/u/infyanurag)\
**Post date:** [May 8, 2013, 1:48pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/12 "2013-05-08T13:48:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/5_5575768a8748004e209b776fc1b2916d.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [May 8, 2013, 1:51pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/13 "2013-05-08T13:51:54Z")

</div>

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

```nohighlight
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 ...

---

<div class="post-metadata">

**Author:** ![Jotne](https://community.unix.com/user_avatar/community.unix.com/jotne/32/2148_2.png) [@Jotne](https://community.unix.com/u/Jotne)\
**Post date:** [May 8, 2013, 3:03pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/14 "2013-05-08T15:03:01Z")

</div>

> [@yoda](#):
>
> ```plaintext
> cal 5 2013 | awk 'NF==7&&NR>2{print $1;exit}'
> 
> ```

Same as I posted here:

> **[Second sunday of March](https://www.unix.com/shell-programming-and-scripting/223249-second-sunday-march.html)**
>
> Hi, I want to get the second Sunday of march in any year, I have tried below command but it is not giving me the correct output pre { overflow:scroll; margin:2px; padding:15px; border:3px inset; margi | The UNIX and Linux Forums

How many threads are needed to solve one problem?

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [May 8, 2013, 3:17pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/15 "2013-05-08T15:17:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jotne](https://community.unix.com/user_avatar/community.unix.com/jotne/32/2148_2.png) [@Jotne](https://community.unix.com/u/Jotne)\
**Post date:** [May 8, 2013, 3:26pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/16 "2013-05-08T15:26:39Z")

</div>

I am very calm 😉

Found this  
[https://bugs.launchpad.net/ubuntu/\+source/bsdmainutils/\+bug/908233](https://bugs.launchpad.net/ubuntu/%5C+source/bsdmainutils/%5C+bug/908233)

Other solution

```plaintext
ncal -m 5 | awk '/Su/ {print $3}'
12

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/5_5575768a8748004e209b776fc1b2916d.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [May 8, 2013, 3:47pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/17 "2013-05-08T15:47:54Z")

</div>

:eek:OMG 11 threads! ELEVEN THREADS!!

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/5_5575768a8748004e209b776fc1b2916d.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [May 8, 2013, 4:08pm UTC](https://community.unix.com/t/how-to-get-first-sunday-of-the-month/328159/18 "2013-05-08T16:08:30Z")

</div>

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!
