Last friday of every month

Hi,
I need to get the date of last friday of every month. how can i achieve this ? please guide me.

Thanks in advance

Using what you can find in this forum:

(My box is an HP hpux11.11)
ant:/home/vbe/wks $ cal_use
ENTER Month Year format mm yyyy 06 2008
CALDATE : 06 2008
Last Friday is 27
ant:/home/vbe/wks $ cal_use
ENTER Month Year format mm yyyy 07 2008
CALDATE : 07 2008
Last Friday is 25
ant:/home/vbe/wks $ cal_use
ENTER Month Year format mm yyyy 02 2008
CALDATE : 02 2008
Last Friday is 29
ant:/home/vbe/wks $

ant:/home/vbe/wks $ more cal_use
#!/usr/bin/ksh

echo " ENTER Month Year format mm yyyy \c "
read CALDATE
echo " CALDATE : " $CALDATE

FRI=$(cal $CALDATE | tail +3 | cut -c16,17 | sed '/^ *$/d' | sed -n '5p')
# or FRI=$(cal $CALDATE | tail +3 | cut -c21,22 | sed '/^ *$/d' | sed -n '5p') under AIX...
if [ "$FRI" -eq "" ]
then
FRI=$(cal $CALDATE | tail +3 | cut -c16,17 | sed '/^ *$/d' | sed -n '4p')
#FRI=$(cal $CALDATE | tail +3 | cut -c21,22 | sed '/^ *$/d' | sed -n '4p') for AIX...
fi
echo "Last Friday is " $FRI

#cal_use: END

All the best

#!/bin/ksh93

year=2008
for month in jan feb mar apr may jun jul aug sep oct nov dec
do
   printf "%(%D)T\n" "last friday in ${month} ${year}"
done

exit 0

outputs

01/25/08
02/29/08
03/28/08
04/25/08
05/30/08
06/27/08
07/25/08
08/29/08
09/26/08
10/31/08
11/28/08
12/26/08

Hi.

The solution by fpmurphy is amazingly compact and on-point for ksh93. If you need more portability, you can use perl:

#!/usr/bin/perl

# @(#) p2       Demonstrate module Date::Manip for last Friday.

use warnings;
use strict;
use Date::Manip;

my ($debug);
$debug = 1;
$debug = 0;

my ( $date, $readable, $unixdate );

if ($debug) {
  print "\nSample:\n\n";
  $date = ParseDate("1st Thursday in June 1992");
  print " first Thursday in June 1992 is :$date:\n";

  $readable = UnixDate( $date, "%F" );
  print "readable, first Thursday in June 1992 is :$readable:\n";
}

my ( $month, $year );
$year = 2008;
my (@months) = qw/ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec /;
foreach $month (@months) {
  $date = ParseDate("last Friday in $month $year");
  if ($debug) {
    print "\n last Friday in $month $year is :$date:\n";

    $readable = UnixDate( $date, "%F" );
    print "readable, last Friday in $month $year is :$readable:\n";
  }
  $unixdate = UnixDate( $date, "%Y.%m.%e" );
  print "$unixdate\n";

}

exit(0);

Producing (in year.month.day format):

% ./p2
2008.01.25
2008.02.29
2008.03.28
2008.04.25
2008.05.30
2008.06.27
2008.07.25
2008.08.29
2008.09.26
2008.10.31
2008.11.28
2008.12.26

Observations: most of the code is debugging (reverse the debug assignments to see some details); you need the perl module Date::Manip (installed as libdate-manip-perl in Debian GNU/Linux); the module is big and slow, but it does the job easily; the perldoc Date::Manip documentation is almost 3000 lines long.

Best wishes ... cheers, drl