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
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
cal -m 03 $i_year | awk 'NR==4{print $NF}'
Jotne
May 8, 2013, 4:35am
3
This gives 9 which is 2 Saturday.
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
cal -m 03 $i_year | awk 'NR>2 && NF==7 {getline;print $1;exit}'
10
Without using cal
#! /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
Yoda
May 8, 2013, 4:59am
5
In KSH93 the built-in printf supports %T formatting option:
#!/bin/ksh93
printf "%(%d)T\n" "2nd sunday march 2013"
Thanks all, I was able to get the result