Last friday of every month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Last friday of every month
# 1  
Old 07-04-2008
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
# 2  
Old 07-04-2008
Using what you can find in this forum:
https://www.unix.com/shell-programmin...h-month-2.html

(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

Last edited by vbe; 07-05-2008 at 07:59 AM.. Reason: add empty line for reading convenience
# 3  
Old 07-04-2008
Code:
#!/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
Code:
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

# 4  
Old 07-04-2008
Hi.

The solution by fpmurphy is amazingly compact and on-point for ksh93. If you need more portability, you can use perl:
Code:
#!/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):
Code:
% ./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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Bash Display First Friday of the next month

Hello, I need to find the date of next first Friday of the month and set as a variable in a bash script ie - FIRSTFRIDAY=$(date -dfirst-friday +%d) I know date -dfirst-friday doesn't work, but unsure if I can use this / cal + awk or something else to find the right date of the... (7 Replies)
Discussion started by: summerdays
7 Replies

2. 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

3. 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

4. Red Hat

How to find/display out last Friday's date of the month?

Hello, Can you please help me find/display out last Friday's date of the month using command in Unix/Linux (3 Replies)
Discussion started by: sunnysthakur
3 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Cron - job to run every 3rd Friday of the month only

Hi Expert Please help me to set a cron job schedule, Ihave a job that run every 3rd Friday of the month at 1030am. I tried to set up like this, but the job still runs every friday at 1030am. I want the job to run every 3rd Friday of the month at 1030am only 30 10 15,16,17,18,19,20,21... (2 Replies)
Discussion started by: kaibiganmi
2 Replies

6. Shell Programming and Scripting

cron job to run on second to last friday each month

I needed a cron job to run on the second to last friday of every month. Our servers are running HP-UX, and the HP-UX date command is pretty basic and does not have all of the fancy options that Linux date command does, and it does not have the ability at all to return future dates. So I had to... (0 Replies)
Discussion started by: lupin..the..3rd
0 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

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

9. Shell Programming and Scripting

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 (10 Replies)
Discussion started by: leslie02
10 Replies

10. Shell Programming and Scripting

Need help, Every friday in a month

I am trying to write a script that shows every Friday in a month. I used cal $1 $2 | grep -v "^$" | awk '{print $6}' It doesn't work for the frist week of Friday because calendar command output has some spaces in the first line and awk '{print $6}' doesn't work. Anybody help me with this... (3 Replies)
Discussion started by: LAY
3 Replies
Login or Register to Ask a Question