commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
Choosing your year and month. You only need the gnu date command and bash. desiredDay of the week is (1..7); 1 is Monday.
If you want desiredDay of week (0..6); 0 is Sunday
desiredDay=6; year=2012; month=5; n=0; while [ $(date -d "$year-$((month+1))-1 - $n day" "+%w") -ne $desiredDay ]; do n=$((n+1)); done; date -d "$year-$((month+1))-1 - $n day" "+%x"
There are 6 alternatives - vote for the best!
If your locale has Monday as the first day of the week, like mine in the UK, change the two $7 into $6
This is a little trickier than finding the last Sunday, because you know the last Sunday is in the first position of the last line. The trick is to use the NF less than or equal to 7 so it picks up all the lines then grep out any empty lines.
If you can do better, submit your command here.
You must be signed in to comment.