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

Dear sunnysthakur,

I'm hoping I have understood the request correctly, so I will have a go with a ksh script.

#!/bin/ksh

day_of_week=`date +%w`          # Sunday is zero
day_of_month=`date +%d`         # Current day of month
year=`date +%Y`                 # Four digit year
month=`date +%m`                # Current month


((delta=$day_of_week+2))

if [ $delta -ge 8 ]             # Is this Friday or Saturday
then
   ((delta=$delta-7))           # Correct for Friday/Saturday
fi

((friday_dom=$day_of_month-$delta))


if [ "$friday_dom" -le 0 ]      # Is this a valid value?
then
   ((month=$month-1))           # Previous month
   if [ $month -eq 0 ]
   then
      month=12
      ((year=$year-1))
   fi
   last_week=`cal $month $year |tail -2|head -1`
   last_day_prev_month="${last_week##* }"
   ((friday_dom=$last_day_prev_month+$friday_dom))    # Actually a subtraction
fi

print "Last Friday was ${friday_dom}, month $month and year $year."

You will need to check that the manual page for date agrees with the options at the top.

There is probably a neat way in perl to convert the date into seconds and calculate there before returning to a date format.

I hope that this helps and the logic I have used is clear.

Robin
Liverpool/Blackburn
UK

The ksh93 built-in printf has %T formatting option:

#!/bin/ksh93

printf "%(%D)T\n" "last friday in Mar 2013"

From ksh93 manual:

A %(date-format)T  format can be use to treat an argument as a date/time string and to format the date/time according 
to the date-format as defined for the date(1) command.
1 Like

This also works for me.

cal 03 2013 | awk 'NR==1 {m=substr($1, 1, 3); y=$2} NF>5 {d=$6} END {print "Friday", m, d, y}'