PDA

View Full Version : How to find/display out last Friday's date of the month



sunnysthakur
March 21st, 2013, 08:29 AM
Hello,

Can you please help me find/display out last Friday's date of the month using command in Unix/Linux

CaptainMark
March 21st, 2013, 08:53 AM
Yes
date --date="last Fri"This will accept arguments found in the man date page, for example
mark@desktop ~/ $ date --date="last Fri" +%x
15/03/13

schragge
March 21st, 2013, 09:19 AM
This will show the last Friday counting from now. What the OP wants is something like what you get in ksh93 with

ksh93 -c 'printf "%(%x)T\n" "last fri in apr 2013"'
or in perl with

perl -MDate::Manip -e 'print UnixDate(ParseDate("last fri in apr 2013"), "%F"),"\n"'

sunnysthakur
March 21st, 2013, 09:26 AM
Yes
date --date="last Fri"This will accept arguments found in the man date page, for example
mark@desktop ~/ $ date --date="last Fri" +%x
15/03/13


This shows previous friday's date...not last friday's date of the month.
I already tried this option...The output date should be 29 march....

sunnysthakur
March 21st, 2013, 09:33 AM
Hi schragge.

Appreciating your help..
this logic works for me.

Is there any method that we don't have to manually change "last fri in apr 2013" every time..just execute the command in any month and will show last day's date of month.

schragge
March 21st, 2013, 09:55 AM
In ksh93, you can simply change the example to

ksh93 -c 'printf "%(%x)T\n" "last fri in this month"'
This doesn't work in perl, better read up the documentation (http://search.cpan.org/~sbeck/Date-Manip-6.39/lib/Date/Manip/Date.pod) and experiment a bit.

Or you can do it like this:

cal -h|cut -c16,17|sed '/^ *$/d'|tail -1

diesch
March 21st, 2013, 10:03 AM
cal -N |awk '/^Fr/ {print $(NF)}' prints just the day of month.
Use something like

date +"%Y-%m-$(cal -N |awk '/^Fr/ {print $(NF)}')" to get a full date.

malspa
March 21st, 2013, 10:34 AM
The output date should be 29 march....


Use something like

date +"%Y-%m-$(cal -N |awk '/^Fr/ {print $(NF)}')" to get a full date.

So then:


$ date +"$(cal -N |awk '/^Fr/ {print $(NF)}') %B"
29 March

ofnuts
March 21st, 2013, 04:23 PM
Bit of a brute force approach using the "nth day" syntax of date: find the Nth day of week in the future that is still the right month:


thismonth=$(date +%m)
for i in {5..0};
do
trymonth=$(date -d "$i friday" +%m)
if [[ $trymonth == $thismonth ]]
then
goodone=$i
break
fi
done
[[ -z $goodone ]] || date -d "$goodone friday"

Of course, plenty of interesting things happen if you are on or past that last day ($goodone empty:)

schragge
March 21st, 2013, 04:35 PM
Why would $goodone be empty on the last Friday? Isn't -d "0 friday" aka -d "this friday" supposed to return the current month number on that day?

ofnuts
March 21st, 2013, 06:35 PM
Because in the last days of the month, '0 friday' could fall in the next month so $trymonth would never be equal to $thismonth. No?

schragge
March 21st, 2013, 06:46 PM
Yes, in the days after the last Friday, but not on that last Friday. No? E.g the last Friday is 29th of this month. Your script will still work on this day, but not on 30th.

CaptainMark
March 21st, 2013, 06:55 PM
Sorry, last fridays date of the month is what you asked for, I see you wanted the date of the last friday in the month

ofnuts
March 21st, 2013, 09:12 PM
Yes, in the days after the last Friday, but not on that last Friday. No? E.g the last Friday is 29th of this month. Your script will still work on this day, but not on 30th.
Hmm, yes. Never tested what happened with "date -d "0 $(date +%A)"". So iterating to 0 can be a solution.

sunnysthakur
March 29th, 2013, 09:10 AM
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}'

schragge
March 29th, 2013, 01:41 PM
Combining this with the code suggested by diesch, I get

ncal -h|awk 'NR==1{m=substr($1,1,3);y=$2}/^Fr/{print"Friday,",m,$NF,y}'
Just for fun, the same in sed:

ncal -h|sed -nr '1h;/^Fr/{G;s/.*( \w+) *\n *(\w{3})\w*/Friday, \2\1/p}'