LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Get first day of last month and last day of last month in bash (http://www.linuxquestions.org/questions/linux-software-2/get-first-day-of-last-month-and-last-day-of-last-month-in-bash-524775/)

xowl 02-01-2007 03:34 PM

Get first day of last month and last day of last month in bash
 
Hi there

I want to get the first day of last month, and the last day of last month in bash.

Any ideas?

Camilo

Example: If in Feb - 27 I run the command, I should get
FirstDay: 01-01-2007
LastDay: 31-01-2007

but If in Mar 3 I run the command, I should get
FirstDay: 01-02-2007
LastDay: 28-02-2007

Thanx in advance

MensaWater 02-01-2007 04:11 PM

Code:

#!/bin/bash
set `date +%m" "%Y`
CURMTH=$1
CURYR=$2

if [ $CURMTH -eq 1 ]
then PRVMTH=12
    PRVYR=`expr $CURYR - 1`
else PRVMTH=`expr $CURMTH - 1`
    PRVYR=$CURYR
fi

if [ $PRVMTH -lt 10 ]
then PRVMTH="0"$PRVMTH
fi


LASTDY=`cal $PRVMTH $PRVYR | egrep "28|29|30|31" |tail -1 |awk '{print $NF}'`

echo First Day: 01-$PRVMTH-$PRVYR
echo Last Day: $LASTDY-$PRVMTH-$PRVYR

Edit: I added the padding for month so now Feb will show as 02 instead of 2.

xowl 02-01-2007 04:13 PM

Hi,


Thanks a lot. It really helped me.

MensaWater 02-01-2007 04:20 PM

No problem.

Make sure you use the edited version I just made. I added a couple of things to it.

xowl 02-05-2007 10:28 AM

Hi, I found a faster way to do this.

I post them here cause it shows how powerful date is:

FirstDay
date -d "-1 month -$(($(date +%d)-1)) days"

Lastday
date -d "-$(date +%d) days -1 month"

rudydark 09-08-2009 04:13 AM

Quote:

Originally Posted by xowl (Post 2617397)
Hi, I found a faster way to do this.

I post them here cause it shows how powerful date is:

FirstDay
date -d "-1 month -$(($(date +%d)-1)) days"

Lastday
date -d "-$(date +%d) days -1 month"

Greetings!
Just tried these above. Mentioned that the Lastday did not worked well. You don't need the -1 month, becouse it just subtracts the days in this month thus gives the last month.

MensaWater 09-08-2009 07:48 AM

Actually neither firstday or lastday worked from command line for me. I hadn't tried them before.

They're also both based on the way bash and gnu date operate. The script I posted above them would work on other UNIX flavors as well as Linux though you'd need to change the interpreter to another shell (e.g. ksh) if no on Linux or a system with bash ported to it.

jlinkels 09-08-2009 10:47 PM

Firstday:
date -d "2007-03-02 -1 month" +%Y-%m-01
2007-02-01

Lastday:
date -d "$(date -d "2007-03-02 -1 month" +%Y-%m-01) +1 month -1 day" +%Y-%m-%d
2007-02-28

jlinkels

MensaWater 09-09-2009 05:59 AM

Why hard code the values like that?

jlinkels 09-09-2009 07:46 AM

Quote:

Originally Posted by jlightner (Post 3675636)
Why hard code the values like that?

As a matter of example. The question was how to find the first and last day of the previous month of a given date.

Obviously you can substitute the hard coded date by a $variable or by "date +%Y-%m-%d" if you like.

Edit: reading your post #7 again, I see that you said "date" didn't work for you on Unix. I missed that, I thought "date" as posted by rudydark didn't work for you, and I thought you added that it would work on GNU only as a sort of general complaint. Oh well, I was wrong,it was late at night yesterday when I wrote my post.

BTW, just because I am interested, why are you (also) running HP-UX, SCO etc? For work, or other reasons?

jlinkels

MensaWater 09-09-2009 12:22 PM

Actually the "date" command exists on HP-UX and all flavors of UNIX I've come across.

What I was saying was the "lastday" and "firstday" posted by someone after my initial post didn't work for me on Linux. I never even tested them before the other poster said they didn't work for him on Linux yesterday.

As to why the other things:
I'm a professional UNIX Administrator and have been since around 1991. The other systems are used in business. I've worked on many UNIX variants such as SunOS (pre-solaris), Solaris, AIX, Xenix, Astrix, Dynix, A/UX (Apple's erstwhile UNIX that ran alongside System 7) and even the original AT&T branded UNIX in addition to the ones we have here. You'd be surprised at the difference in some of the tools between the UNIX variants. The GNU tools of the same name often have far more functionality than the UNIX tools. On Dynix I had to write myself a fake finger program (or "last" - it's been a while) because it didn't exist and I needed it for a system gather script I was writing for all platforms.

bonnyfused 11-23-2009 04:54 AM

Quote:

Originally Posted by xowl (Post 2617397)
Hi, I found a faster way to do this.

I post them here cause it shows how powerful date is:

FirstDay
date -d "-1 month -$(($(date +%d)-1)) days"

Lastday
date -d "-$(date +%d) days -1 month"

Are following OK for FIRST DAY CURRENT MONTH and LAST DAY CURRENT MONTH?

I tried them and they seemed OK, just would like to get your confirmation:

First day current month
date -d "-$(($(date +%d)-1)) days"

Last day current month
date -d "+1 month -$(date +%d) days"

Thanks!

MensaWater 11-23-2009 08:44 AM

Yes it works for me as well.

bonnyfused 11-23-2009 10:44 AM

Quote:

Originally Posted by jlightner (Post 3766723)
Yes it works for me as well.

Thanks for your reply.

Now a bit more "difficult": I'm using aforementioned one-liners in a script.
I'm running this script every evening at 20:30 and I collect data from the day before. This means:

today (23/11/2009) at 20:30 I'll collect some data and save them in a txt file as:

22/11/2009,dataA,dataB

After having done this, I use gnuplot to automatically generate my histogram of that collected values. Therefore, I have always the histogram for "yesterday".
Now, as I need to define the x-range everytime I run this script, and I want it to be FIRST DAY OF MONTH --> LAST DAY OF MONTH, I'm a bit stuck on how to handle the first day of a month.
This is because of following.
Let's assume it's the first of december (01.12.2009). I'm collecting data from 30.11.2009 and would like to plot it within the boundaries of NOVEMBER. Thus, the one-liner for the "LAST_DAY" is not working anymore, because it would return me "31.12" instead of my desired "30.11" (and also the first day would be wrong, with the above command, but that can be solved easily).

How would you suggest me to do it, considering that in gnuplot there is no "if" possibility?

Thanks...

jach 08-09-2012 05:52 AM

FirstDay
date -d "-1 month -$(($(date +%d)-1)) days"

Won't work on the 9th day of the month as $(( tries to subtract 09-1. Any number with 0 in front of it is interpreted as an octal number and there is no octal 9.

You will get the following error
09: value too great for base (error token is "09")

Solutions:

# +%-d will remove the zero padding
date -d "-1 month -$(($(date +%-d)-1)) day"

# separate the arithmetic
date -d "-1 month -$(date +%d) day +1 day"

# change 09 into 9 decimal with 10#
date -d "-1 month -$((10#$(date +%d) - 1)) day"

Lastday
date -d "-$(date +%d) day"


All times are GMT -5. The time now is 12:03 AM.