Second sunday of March


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Second sunday of March
# 1  
Old 05-08-2013
Second sunday of March

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
Code:
i_year=`date +%Y`
cal -m 03 $i_year | sed  '/^$/d' |head -4 |tail -1|rev | cut -c1`

This is returning me 0 , where as I want 10.
Can you please help

Thanks

Last edited by Franklin52; 05-08-2013 at 05:27 AM.. Reason: Please use code tags
# 2  
Old 05-08-2013
Code:
cal -m 03 $i_year |  awk 'NR==4{print $NF}'

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 05-08-2013
This gives 9 which is 2 Saturday.

Code:
     March 2013
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

Script should also work for other years too

Edit: this seems to work
Code:
cal -m 03 $i_year | awk 'NR>2 && NF==7 {getline;print $1;exit}'
10


Last edited by Jotne; 05-08-2013 at 05:43 AM..
This User Gave Thanks to Jotne For This Post:
# 4  
Old 05-08-2013
Without using cal Smilie
Code:
#! /bin/bash

yr=2013
i=1

while [ $i -le 7 ]
do
    d=`date -d "03/${i}/${yr}" +%a`
    if [ "$d" == "Sun" ]
    then
        echo "Second Sunday of March in $yr falls on $(( $i + 7 ))"
        break
    fi
    (( i++ ))
done

# 5  
Old 05-08-2013
In KSH93 the built-in printf supports %T formatting option:
Code:
#!/bin/ksh93
printf "%(%d)T\n" "2nd sunday march 2013"

# 6  
Old 05-08-2013
Thanks all, I was able to get the result
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Yesterday's Date if it is Sunday

Hi All, Can you please let me know how to get the yesterday's date of the given date if the given date is Sunday? I can't use GNU. I have the code to get the yesterday's date based on the system date. Thanks (5 Replies)
Discussion started by: shash
5 Replies

2. Shell Programming and Scripting

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. ... (17 Replies)
Discussion started by: infyanurag
17 Replies

3. UNIX for Advanced & Expert Users

How to get the count of sunday between two dates?

Hi Am using unix Ksh I have the two dates DATE1=01/01/2013 DATE2=11/02/2013 In this two dates i need the output as count of sunday sunday=6 Can anyone help me pls!!! (1 Reply)
Discussion started by: Venkatesh1
1 Replies

4. UNIX for Advanced & Expert Users

How to get the sunday days between two dates?

Hi Am using Unix Ksh I have a two date input as DATE1=02/12/2012 DATE2=30/12/2012 I Need the output as only sunday date 02/12/2012 09/12/2012 16/12/2012 23/12/2012 30/12/2012 can anyone pls help me.. thanks in advance... (2 Replies)
Discussion started by: Venkatesh1
2 Replies

5. Shell Programming and Scripting

Cron for an alternate sunday

Hi again, I need to delete stats on every 2nd Sunday and I've try the following codes but unfortunately it didn't work. I'll really appreciate as always for your help. I'm on FreeBSD 8.2-STABLE. altsun.sh script: #!/bin/sh day = $(`date '+%e'`) if then rm... (9 Replies)
Discussion started by: user`
9 Replies

6. UNIX for Dummies Questions & Answers

Run script on every second sunday

I was to schedule a script in a crontab after every 15 days specically on every 2nd Sunday. I know that i can schedule on basis of weekdays, but can it be done by skipping in between???:wall: (5 Replies)
Discussion started by: masquerer
5 Replies

7. UNIX for Dummies Questions & Answers

march for my CPU

Hello, guys ! I tried to compile PHP on one of my servers (LAMP). Everything worked fine only that I tried to set my march to pentium4 and it did not accepted it. It told me that it is a x86_64 architecture and pentium4 is not supported (I don't remember the exact error, but this was the ideea).... (2 Replies)
Discussion started by: Sergiu-IT
2 Replies

8. Solaris

How to check for Saturday or Sunday

Hi , I have a date parameter passed in YYYYMMDD format , how can I check whether it is Sat or Sun on Solaris box , as we can do the same easily on linux box by using date -d YYYYMMDD '+a' . Any pointres will be really helpful . (5 Replies)
Discussion started by: harpreetanand
5 Replies

9. Shell Programming and Scripting

Date - Last Sunday thru Saturday

Hi All, I have to kick off a script on every Monday to get some data from database for last week (Sunday thru Saturday). If its Monday (12/12/2005) Begin date will be Sunday - 12/4/2005 End date will be Saturday - 12/10/2005 The script might not kick off on some Mondays. So my... (2 Replies)
Discussion started by: skymirror
2 Replies
Login or Register to Ask a Question