Counting number of Sundays in a given month with BASH


I need to count number of Sundays for a given month. For example if I want to get number of Sundays in DEC 2011 I do:

$ ncal -m 12 | grep Su | echo $(( `wc -w` - 1))

this returns 4. All good so far but I also need to do it for next year and I do not see to find an option for ncal to output only single month instead of whole year:

$ ncal -m 12 -y 2012

Is there any other way to get same result for next year without over-complicating things?

thank you


SUNDAYS=ncal -y 2012 | grep -i su | wc -w; FINAL=$(($SUNDAYS/12)); echo $FINAL

Hope I understood correctly what you wanted, since I’m a bit ill and not very smart right now.


Not really.

your command returns average Sundays per month for year 2012 = 4. The problem is I need this on per month basis so I would know how many Sundays are in the single month. There maybe 4 or 5 depends on the month. For example this is easy to do with ncal for a current year 2011:

$ ncal -m 12 | grep Su | echo $(( `wc -w` - 1))
$ ncal -m 10 | grep Su | echo $(( `wc -w` - 1))

Dec 2011 - 4 Sundays
Oct 2011 - 5 Sundays

How do I do the same but for year 2012 using bash? The problem is that ncal displays all months for year other than current.


ncal 4 2016 | grep Su | echo $(( wc -w - 1))

For April 2016