Hello,
Can you please help me find/display out last Friday's date of the month using command in Unix/Linux
Hello,
Can you please help me find/display out last Friday's date of the month using command in Unix/Linux
YesThis will accept arguments found in the man date page, for exampleCode:date --date="last Fri"Code:mark@desktop ~/ $ date --date="last Fri" +%x 15/03/13
Last edited by CaptainMark; March 21st, 2013 at 08:55 AM.
Catch me on Freenode - imark
This will show the last Friday counting from now. What the OP wants is something like what you get in ksh93 with
or in perl withCode:ksh93 -c 'printf "%(%x)T\n" "last fri in apr 2013"'
Code:perl -MDate::Manip -e 'print UnixDate(ParseDate("last fri in apr 2013"), "%F"),"\n"'
Last edited by schragge; March 21st, 2013 at 08:42 PM.
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.
In ksh93, you can simply change the example to
This doesn't work in perl, better read up the documentation and experiment a bit.Code:ksh93 -c 'printf "%(%x)T\n" "last fri in this month"'
Or you can do it like this:
Code:cal -h|cut -c16,17|sed '/^ *$/d'|tail -1
Last edited by schragge; March 21st, 2013 at 10:03 AM.
prints just the day of month.Code:cal -N |awk '/^Fr/ {print $(NF)}'
Use something like
to get a full date.Code:date +"%Y-%m-$(cal -N |awk '/^Fr/ {print $(NF)}')"
ClassicMenu Indicator - classic GNOME menu for Unity
Unsettings - configuration program for the Unity
Privacy Indicator - easily switch privacy settings in Unity
Arronax - create and modify app starters
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:
Of course, plenty of interesting things happen if you are on or past that last day ($goodone emptyCode: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"
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?
Bookmarks