Results 1 to 9 of 9

Thread: Getting the last saturday of the month

  1. #1
    Join Date
    Feb 2008
    Location
    Belgium
    Beans
    53
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    [solved] Getting the last saturday of the month

    Hi,

    I would like to get the date of the last saturday of the month in Bash.
    If we look to this month (june 2009) it would be "27".

    I have this (week is starting on monday):

    Code:
    saveFullMonthOn='6'
    lastXDay=`cal | awk {'print $saveFullMonthOn'} | xargs | /usr/bin/cut -d" " -f5`
    But that is printing "We" instead "27".
    Whats going wrong?

    Thanks.
    Last edited by Scormen; June 7th, 2009 at 12:52 PM.

  2. #2
    Join Date
    Feb 2008
    Location
    Belgium
    Beans
    53
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Getting the last saturday of the month

    Problem solved:

    saveFullMonthOn='6'
    lastXDay=`cal | awk {"print "'$'"$saveFullMonthOn"} | xargs | /usr/bin/cut -d" " -f5`

  3. #3
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Getting the last saturday of the month

    Quote Originally Posted by Scormen View Post
    Problem solved:
    that will print 26 instead of 27.

    here's another way, with only gawk
    Code:
    awk 'BEGIN{
     for(i=1;i<=31;i++){
        day=sprintf("%02d",i)    
        d="2009 06 "day" 00 00 00"
        if ( strftime("%a",mktime(d)) == "Sat"){
         lastsat=day
        }
     }
     print "Last sat is: " lastsat
    }'

  4. #4
    Join Date
    Feb 2008
    Location
    Belgium
    Beans
    53
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Getting the last saturday of the month

    Is your week starting on sunday mabye?

  5. #5
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Getting the last saturday of the month

    Quote Originally Posted by Scormen View Post
    Is your week starting on sunday mabye?
    does it matter?

  6. #6
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Getting the last saturday of the month

    Scormen, the command
    Code:
    lastXDay=`cal | awk {"print "'$'"$saveFullMonthOn"} | xargs | /usr/bin/cut -d" " -f5`
    only works on certain months because

    Code:
    cut -d" " -f5
    will only work when the last saturday happens to be the fourth saturday of the month.
    Some months like May 2009 had five saturdays.

    An alternative to ghostdog74's solution would be

    to save this as ~/bin/last_sat
    PHP Code:
    #!/usr/bin/env python
    import sys
    import datetime
    from dateutil
    .relativedelta import relativedelta,SA
    print (datetime.date(int(sys.argv[1]),int(sys.argv[2]),1)+relativedelta(day=31,weekday=SA(-1))).day 
    make it executable (of course)
    PHP Code:
    chmod 755 ~/bin/last_sat 
    and use it in your script like this:
    PHP Code:
    lastXDay=$(last_sat 2009 $saveFullMonthOn)
    echo 
    $lastXDay 
    In general, "last_sat YEAR MONTH" will return the day of the last saturday in year YEAR and month MONTH.

    Or, as a one-liner:

    PHP Code:
    lastXDay=$(python -'import sys;import datetime;from dateutil.relativedelta import relativedelta,SA;print (datetime.date(int(sys.argv[1]),int(sys.argv[2]),1)+relativedelta(day=31,weekday=SA(-1))).day' 2009 6
    Last edited by unutbu; June 7th, 2009 at 01:49 PM.

  7. #7
    Join Date
    Feb 2008
    Location
    Belgium
    Beans
    53
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Getting the last saturday of the month

    Sometimes?

    My output is realy:
    root@test:/home/kris# ./test.sh
    27
    Edit: unutbu was first.

    @unutbu:
    Thank, but I don't know anything about python, but I understand what you mean.

    What do you think about this one?
    Code:
    cal | awk "{ if(\$6!=\"\") x=\$6; } END { print x }"
    Last edited by Scormen; June 7th, 2009 at 01:49 PM.

  8. #8
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Getting the last saturday of the month

    ghostdog74 knows much more about awk than I do, so I would listen to his opinion.
    That said,
    Code:
    cal -m | awk "{ if(\$6!=\"\") x=\$6; } END { print x }"
    looks like a good solution to me. (I added the -m flag so cal will make Monday the first day of the week. That way your script will work independent of locale.)

  9. #9
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Getting the last saturday of the month

    Quote Originally Posted by Scormen View Post
    Sometimes?

    My output is realy:


    Edit: unutbu was first.

    @unutbu:
    Thank, but I don't know anything about python, but I understand what you mean.

    What do you think about this one?
    Code:
    cal | awk "{ if(\$6!=\"\") x=\$6; } END { print x }"
    don't have to put slashes
    Code:
    cal -m | awk '$6!=""{x=$6;} END { print x }'
    also, no need the if statement.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •