1848155 Members
8528 Online
104022 Solutions
New Discussion

Days between, awk?

 
SOLVED
Go to solution
Neil Edwards
Advisor

Days between, awk?

Hello awk experts,

Anybody know of an easy way to count the number of days between two dates like 7/1/2002 and 6/1/2001? I searched the forums and A. Clay's caljd.sh but it wants separate variables for month, day, and year. I was hoping someone already had an awk program to split the dates apart. Awk is not one of my strongpoints.

Thanks in advance,
Neil
It wasn't me.
7 REPLIES 7
Patrick Wallek
Honored Contributor
Solution

Re: Days between, awk?

Check out A. Clay's newest version of caljd.sh (and caljd.pl) at this thread.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x1fb33a7b3682d611abdb0090277a778c,00.html

This one allows you to specify a date as one string. Hopefully this'll help you.
A. Clay Stephenson
Acclaimed Contributor

Re: Days between, awk?

Hi Neil:

Patrick pointed you to the latest version, 2.1, which will do this internally. If you had asked this a couple of weeks ago the answer would have been a simple awk script but due to a 'customer' request; your all set. Unfortunately your usage to not make my examples list. I might add it later after I see a few more typical date questions.

This should fix you, if and only if, you use caljd.sh (or caljd.pl) version 2.1.
---------------------------------------

D1="7/1/2002"
D2="6/1/2001"
DIFF=$(( $(caljd.sh -S "/" -c ${D1} - $(caljd.sh -S "/" -c ${D2}) ))
echo "Days = ${DIFF}"

If I counted those pesky paren's correctly, that shoulf fix you.

Regards, Clay

If it ain't broke, I can fix that.
Patrick Wallek
Honored Contributor

Re: Days between, awk?

I think Clay missed a close paren in there.

The line:
DIFF=$(( $(caljd.sh -S "/" -c ${D1} - $(caljd.sh -S "/" -c ${D2}) ))

Should be (I think):
DIFF=$(( $(caljd.sh -S "/" -c ${D1}) - $(caljd.sh -S "/" -c ${D2}) ))
A. Clay Stephenson
Acclaimed Contributor

Re: Days between, awk?

Hi again:

I just realized that your question was a bit ambiguous in that 7/1/2002 might be July 1, 2002 or it might be 7 January, 2002 especially if you was one of them folks what lives way over yonder. If that is the case then you need to add the -e (European) option to both caljd.sh calls.

Regards and no more points for this, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Days between, awk?

Hi again:

Patrick is indeed correct. My advanced one-finger hunt-and-peck method has failed me once again. That there boy is smarter than I am.
If it ain't broke, I can fix that.
Patrick Wallek
Honored Contributor

Re: Days between, awk?

I don't think I'd go that far Clay, but thanks for the compliment anyway.

(No points for this)
Neil Edwards
Advisor

Re: Days between, awk?

Thanks!!! You guys are great. I had an answer in under 5 minutes!!!

Clay, I had your earlier version but this date "sledgehammer" is great. I especially liked your examples section.

Thanks again to both of you,
Neil
It wasn't me.