how can i find the third friday of each month?

Help please! I need to read the calendar and put the date of the third Friday of each month into a variable for comparison in an "if" statement. How would I do this?

Thnx,
leslie02

The third friday is always between days 15 and 21 inclusive of the month .
You can made the test like that :

day_of_week=$(date +%w)  # (0..6); 0 is Sunday, 5 is Friday
day_of_month=$(date +%e) # space padded
if [ ${day_of_week} -eq 5 -a ${day_of_month} -ge 15 -a ${day_of_month} -le 21 ]
then
   echo "third Friday "
fi

Jean-Pierre.

Leslie,
See if this works for you:

typeset -i mMth=1
mYear='2007'
while [ ${mMth} -le 12 ]
do
  m3Friday=`cal ${mMth} ${mYear} | tail +3 | cut -c16,17 | sed '/^ *$/d' | sed -n '3p'`
  echo "Third Friday of "${mMth}"/"${mYear}" = "${m3Friday}
  mMth=${mMth}+1
done

This should work

Thanks
Ashok

cal 07 2007 | cut -c16-18 | tail -4 | head -1

cal 05 2007 | sed -n "5{s/ *[0-9]\{2\}$//;s/^.* //p;}"

Shell_Life, akrathi, anbu23, it seems that your solutions do not work in all the cases :

$ cal 12 2007
       December 2007      
Sun Mon Tue Wed Thu Fri Sat  
                         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 12 2007 | tail +3 | cut -c16,17 | sed '/^ *$/d' | sed -n '3p'
 2
$ cal 12 2007 | cut -c16-18 | tail -4 | head -1
 13
$ cal 12 2007 | sed -n "5{s/ *[0-9]\{2\}$//;s/^.* //p;}"
14
$

A possible solution :

$ cal 12 2007 | awk 'NR>2 && NF>=2 && ++w==3 {print $6}'
21
$ cal 07 2007 | awk 'NR>2 && NF>=2 && ++w==3 {print $6}'
20
$ cal 6 02007 | awk 'NR>2 && NF>=2 && ++w==3 {print $6}' 
15
$

Jean-Pierre.

this one doesn't use cal

count=0
month="05"
for s in `seq 1 31`
do
 fr=$(date +%a --date="2007-$month-$s" )
 if [ "$fr" == "Fri" ];then
     friday[$count]=$s
     count=$((count+1))
 fi 
done
echo ${friday[2]} #third fri

Thanks everyone! Your creativity and helpfulness is fantastic. :slight_smile:

leslie

Aigles,
My solution does work and it produces 21 as the answer:
I am using kshell on SunOS.

cal 12 2007 | tail +3 | cut -c16,17 | sed '/^ *$/d' | sed -n '3p'

I am using ksh on AIX.
I think that the output of the cal command is not the same on AIX and SunOS.
On AIX, the output for December 2007 is :

$ cal 12 2007
       December 2007      
Sun Mon Tue Wed Thu Fri Sat  
                         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

For me, the column for Friday is in positions 21-22, not 16-17.

Jean-Pierre.

Aigles,
Following is the output from the "cal" of the SunOS.
Important notes:
1) The positions 16 and 17 are indeed for Friday.
2) There is only 1 (one) space in between the days.
As a side note, even the header with the abbreviated name of
the days are different.

   December 2007
 S  M Tu  W Th  F  S
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
....+....1....+....2 <==============
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31