1753508 Members
4944 Online
108795 Solutions
New Discussion юеВ

Date format

 
SOLVED
Go to solution
Fenglin
Regular Advisor

Date format

Hi

MTHLESS="$((`date +%m`-1))"

using the above command,I am able to get the current month -1, I would like to know whether it will work when it comes to January. Will I be able to get December as the output?

Please advice.

Regards
Feng Lin
13 REPLIES 13

Re: Date format

That is just a simple arithmetic calculation, so in January it will resolve as follows:

01 - 1 = 0

Which I suspect is not what you want...

Why not install the GNU version of date which is available in the GNU coreutils package available here:

http://hpux.connect.org.uk/hppd/hpux/Gnu/coreutils-7.4/

(Don't miss the runtime dependencies you need too)

Once you have that you can easily do things like this:

# /usr/local/coreutils/bin/date +%m
07
# /usr/local/coreutils/bin/date -d "last month" +%m
06


HTH

Duncan

I am an HPE Employee
Accept or Kudo
Dennis Handly
Acclaimed Contributor

Re: Date format

>Will I be able to get December as the output?

You can get 12 if you do:
(( MTHLESS = ($(date +%m) + 11) % 12 + 1 ))
James R. Ferguson
Acclaimed Contributor

Re: Date format

Hi:

If the month is all that you want, you can do:

# awk -v $(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;print MON}'

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Date format

>JRF: awk -v $(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;print MON}'

I'm not sure how using awk is better than a real shell's builtin arithmetic expressions using clock arithmetic (%)?
Also, I think you need to redirect stdin to /dev/null or add ";exit" to the end.
James R. Ferguson
Acclaimed Contributor

Re: Date format

Hi (again):

> Dennis: I'm not sure how using awk is better than a real shell's builtin arithmetic expressions using clock arithmetic (%)?

I never said that is was. I'm merely pointing out TIMTOWDI ;-)

> Dennis: Also, I think you need to redirect stdin to /dev/null or add ";exit" to the end.

The BEGIN rule takes care of that _if_ I hadn't bungled the variable assignment:

# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;print MON}'

Regards!

...JRF...
Fenglin
Regular Advisor

Re: Date format

Hi All

Thanks for the info.

Now I have the following situation : -

I have files with naming convention *yyyymmdd*. How am I going to take care of the zero(Jan - Sep) and one(Oct - Dec) for the month if I need to do housekeeping on those files?

I intend to run the housekeeping job twice a month on the 1st and 16th of the month. 1st is to archive 16 - 31st day files, 16th is to archive 1 - 15th day files.

Thanks a lot.

Regards
Feng Lin
James R. Ferguson
Acclaimed Contributor

Re: Date format

Hi (again):

> How am I going to take care of the zero(Jan - Sep) and one(Oct - Dec) for the month if I need to do housekeeping on those files?

# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}'
06

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor
Solution

Re: Date format

>JRF: The BEGIN rule takes care of that _if_ I hadn't bungled the variable assignment:

I guess the way awk works is that it doesn't bother reading the input files if there are no pattern-action statements, other than BEGIN.
I don't know if this is documented, so I wouldn't depend on it. The standard seems to say that if NO actions and patterns, then the files aren't read.

>How am I going to take care of the zero (fill) (Jan - Sep) and one(Oct - Dec) for the month

typeset -Z2 MTHLESS # zero fill
(( MTHLESS = ($(date +%m) + 11) % 12 + 1 ))
echo $MTHLESS
James R. Ferguson
Acclaimed Contributor

Re: Date format

Hi (again):

> Dennis: I guess the way awk works is that it doesn't bother reading the input files if there are no pattern-action statements, other than BEGIN. I don't know if this is documented, so I wouldn't depend on it. The standard seems to say that if NO actions and patterns, then the files aren't read.

I agree that the input file(s) are not read in the case of a BEGIN block with no pattern-action statements in the subsequent main block. This is easy to demonstrate.

Of course, this behavior doesn't occur if an END block is declared either alone or in concert with a BEGIN block. In that case, at least one file must be input.

I concede that relying on these subtitles may lead to confusion and/or false expectations. Hence it is probably best to amend the 'awk' snippet to:

# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}' /dev/null

...and then (similarly) this works too:

# awk -v MON=$(date +%m) 'END{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}' /dev/null

Regards!

...JRF...