Javid Jamae Javid Jamae - 1 month ago 7
Linux Question

The last Saturday of a given month / year

In Unix, I want to find the date (day of month) of the last Saturday of a given month and year. I know

cal
will give me the calendar for a given month / year. What is a good way to get the last Saturday?

Update: I'd like a solution that I could use to apply to any day of the week. For example, I could use the same method to find the date of the last Sunday or Wednesday as well.

Answer

Use awk for that. Consider this:

cal 5 2013 | awk 'NF>6{a=$7} END{print a}'

OUTPUT:

25
Comments