How to get first sunday of the month?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get first sunday of the month?
# 8  
Old 05-08-2013
Quote:
Originally Posted by infyanurag
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.
This User Gave Thanks to durden_tyler For This Post:
# 9  
Old 05-08-2013
Code:
cal 5 2013 | awk 'NF==7&&NR>2{print $1;exit}'

This User Gave Thanks to Yoda For This Post:
# 10  
Old 05-08-2013
Thanks Yoda, it worked, can we also use it to get the second sunday?
I mean to get it generalized?
# 11  
Old 05-08-2013
Here is a generalized approach for sunday:
Code:
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.
This User Gave Thanks to Yoda For This Post:
# 12  
Old 05-08-2013
Many Thanks to all, especially Yoda. Now I have learnt how to do this by your help
# 13  
Old 05-08-2013
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
Code:
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 ...
# 14  
Old 05-08-2013
Quote:
Originally Posted by Yoda
Code:
cal 5 2013 | awk 'NF==7&&NR>2{print $1;exit}'

Same as I posted here:
https://www.unix.com/shell-programmin...day-march.html

How many threads are needed to solve one problem?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

cron job on 3rd sunday of every month

Hi, Actually scheduled my test scripts on test severs as shown below. They are supposed to run on 3rd sunday of every month. Unfortunately it ran on 2nd sunday of the month (suspecting that it will run every sunday). I am sorry if I miss something. Could you please let me know if I did any... (1 Reply)
Discussion started by: System Admin 77
1 Replies

2. Shell Programming and Scripting

Run script on every last sunday of the month

Hi ALL, I have been testing this script to run for every last Sunday of the month,looks like month which have 5 sunday (july 2016 )is not passing this and failing every time. Here is what I am using, current_date=31 echo " CURRENT DAY -> $current_date" if ... (2 Replies)
Discussion started by: netdbaind
2 Replies

3. Shell Programming and Scripting

Trigger the script if it is first sunday of the quarterly month

Hello All, I have script which needs to be scheduled in crontab which needs to be run only on the first sunday of the quarterly months (March, June, September,Dec). Can anyone please help me out on this. Thanks, Arun. (13 Replies)
Discussion started by: Arun1992
13 Replies

4. Shell Programming and Scripting

How to setup cronjob 3rd sunday of every month?

Hi All, I need to set up cronjob for every third sunday of the month. here i have seen one example for 4th sunday for every month in another post and it looks perfect.can anyone please help me to understand this and help me to get the setup for third sunday of every month.Thanks. this is... (7 Replies)
Discussion started by: netdbaind
7 Replies

5. Shell Programming and Scripting

Need last month files after 10th of every month

Hi, I need all file names in a folder which has date >= 10th of last month, Example : files in folder AUTO_F1_20140610.TXT BUTO_F1_20140616.TXT CUTO_F1_20140603.TXT FA_AUTO_06012014.TXT LA_AUTO_06112014.TXT MA_AUTO_06212014.TXT ZA_AUTO_06232014.TXT Output: AUTO_F1_20140610.TXT... (9 Replies)
Discussion started by: nani1984
9 Replies

6. Shell Programming and Scripting

How to add decimal month to some month in sql, php, perl, bash, sh?

Hello, i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m i need to write this script in php or bash or sh or mysql or perl in normal time o unix time i`m asking or there are any simple way to add partial number of month to some... (14 Replies)
Discussion started by: bacarrdy
14 Replies

7. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

8. UNIX for Dummies Questions & Answers

How to Set up a cronjob On 4th Sunday of every Month?

How to Set up a cronjob which will run On 4th Sunday of every Month at 8:00 PM :( (11 Replies)
Discussion started by: tp2115
11 Replies

9. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

10. UNIX for Dummies Questions & Answers

Cron job -- to execute at every first Sunday of every month

Dear all How can I schedule the cronjob to be run sometime at every first Sunday at every month? I have the edit the cronjob every month now, thanks (2 Replies)
Discussion started by: shanemcmahon
2 Replies
Login or Register to Ask a Question