1855366 Members
2848 Online
104110 Solutions
New Discussion

Comparing the months....

 
Purusa
Frequent Advisor

Comparing the months....

Hi all,

Using shell script and awk statements, I manage to get the month and year of the Patch Bundle installed on the system. Now I have to compare month and year of Patch Bundle installed to the Patch Bundle which is desired on the workstation. I am able to do easily comparison of year but not of month. Can anyone suggest how to do this???

In short I have to accomplish:
if [ "march" greater than "november" ];
then
echo "jjjjjjjj"
else
echo "iiiiiiii"
fi

Thanx for your cooperation and help
Regards,
Pankaj
A deep chasm can't be crossed in two steps
13 REPLIES 13
Karthik S S
Honored Contributor

Re: Comparing the months....

you can do something like this,

january=1
february=2
...
december=12

then compare using if statements.

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Steve Steel
Honored Contributor

Re: Comparing the months....

Hi


Thats good advice

case $month in
January ) let realmonth=1 ;;
---
December ) let realmonth=12 ;;
esac

Same for wantedmonth

Compare 2 results as decimal

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Mark Grant
Honored Contributor

Re: Comparing the months....

Just to be different

$datestring="january1 february2 march3 april4 etc etc"

$month=`expr "$datestring" : ".*\$monthtest\(.*\) .*"`

This will return the month number.

Never preceed any demonstration with anything more predictive than "watch this"
Purusa
Frequent Advisor

Re: Comparing the months....

Hi Mark,

I am exploring the approach suggested by you as it is quite compact and presentable. Can you just explain it further???

Thanx
Pankaj
A deep chasm can't be crossed in two steps
Mark Grant
Honored Contributor

Re: Comparing the months....

Well, expr matches a strings against another string. As an exciting by-product it will return any part of the match that is in brackets. So, assuming $monthtest contains "march", "expr" will look in $datestring for the characters "march" followed by some characters up to a space (because that's what we've asked it to match). Because of the exciting by-product mentioned above, it will return everything from after "march" to the first which, will be a number (because that's what we have in $datestring.

You then just do this as as subroutine or explicitly like

[ `expr "$datetest" : ".*$month1\(.*\) .*`" -gt `expr "$datetest" : ".*$month2\(.*\) .*`" ] && {
# code if month 1 is greater than month 2
}

You can use "if" if you prefer.

It would have been much more compact in perl though.
Never preceed any demonstration with anything more predictive than "watch this"
Purusa
Frequent Advisor

Re: Comparing the months....

Hi Mark,

Thats great. The fix is working fine, however, a small hitch is still there. Instead of stopping at the month number i.e. being stopped by a "space" character, it is going ahead.
For monthtest = august, the output is:
*************************************
8 september9 october10 november11 december12
*************************************
I am trying different combinations of the fix but somehow I am not able to crack it and get only the number "8" as display.

Waiting for response,
Pankaj
A deep chasm can't be crossed in two steps
Mark Grant
Honored Contributor

Re: Comparing the months....

Thats because I made a slight mistake!!

Change the expr to look like the following!

expr "$datestring" : ".*$monthtest\([[:digit:]]\)"

Apologies!
Never preceed any demonstration with anything more predictive than "watch this"
Kent Ostby
Honored Contributor

Re: Comparing the months....

In awk, add the following into a BEGIN statement at the start of the awk script:

BEGIN{themonth["january"]=1; etc }
Then when you print out the month and year from the patch bundle, instead of printing out the fields (say field 1 was the month and field 2 was the year), you would print out themonth[$1], $2.

Best regards,

Oz
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Purusa
Frequent Advisor

Re: Comparing the months....

Hi Mark,

That seems to be working fine now. It gives me directly the month no as the output.
However there is a small issue again :-)
This time it works fine only if the output is single digit i.e 1 to 9. But if the month is october or nov or dec then it displays only "1". So how to go about this thing.

Also can you tell me some link/resource as to where can I get the info about the syntax you are using over here like " [[:digit:]]" etc for my better understanding. I will be then using this with logic and not blindly:-)

Thanx for your timely support...
Pankaj
A deep chasm can't be crossed in two steps
A. Clay Stephenson
Acclaimed Contributor

Re: Comparing the months....

Let me give you the perfect way to solve this. The idea is to take your monthname and your year and we will simply use the 1st day of any month as our input dates. We will get out a julian day. Do this for both month/year tuples and you now have two numbers to compare.

For example:
MONTH1="march"
YEAR1=2003

MONTH2="July"
YEAR2=2003

JDATE1=$(caljd.sh -I ${MONTH1} 1 ${YEAR1})
JDATE2=$(caljd.sh -I ${MONTH2} 1 ${YEAR2})

if [[ ${JDATE1} -gt ${JDATE2} ]]
then
echo "${MONTH1} ${YEAR1} is later then ${MONTH2} ${YEAR2}"
else
echo "${MONTH2} ${YEAR2} is later than or equal to ${MONTH1} ${YEAR1}"


The -I tells caljd.sh that full case-insensitive monthnames (rather than numeric) are expected. Similarly -i tells caljd.sh that abbreviated monthnames are expected. For a listing of the monthnames, do a locale LC_TIME.

As a bonus, because the locale is utilized. If LANG is properly set, non-English monthnames work without changes -- something that would clobber the other methods.

Invoke as caljd.sh -u for full usage.



Here is caljd.sh, version 2.22.
If it ain't broke, I can fix that.
Mark Grant
Honored Contributor

Re: Comparing the months....

Although I would happen to agree that A.Clay has, assuming perfection is finite, the perfect solution, I'll just answer your last query.

The "[[:digit:]]" matches one digit. It should mathc one or more digits. We can achieve that by add an * after it soit starts to look like this.

expr "$datestring" : ".*$monthtest\([[:digit:]]*\)"

If I actually concentrated a bit more, perhaps I could have saved us all a bit of time by getting it right in the first place :)

Anyway, you can start out on the wonderful road that is "regular expressions" by looking at the man page "man 5 regexp". This isn't easy to follow the first time you read it so you'll have to be a bit patient and ask questions on the forum if you have problems.

The great thing about regular expressions is that they turn up in so many places and understanding them can solve so many problems. But an even better thing about learning them is that when you decide you want to start learning perl, you won't get scared off by the long (and usually quite early) chapters on perl regular expressions which take the whole subject to an almost ridiculous level.
Never preceed any demonstration with anything more predictive than "watch this"
Purusa
Frequent Advisor

Re: Comparing the months....

Hi,

Thanx for cooperation of you all.
Mark, I had guessed that '*' later on and code is behaving nicely.

Thanx to A.Clay for the wonderful 'perfect' piece of code.
We can close this now.... :-)

Regards,
Pankaj
A deep chasm can't be crossed in two steps
Hein van den Heuvel
Honored Contributor

Re: Comparing the months....



Just to be more different...

$ month='Aug'
$ echo `expr 'JanFebMarAprMayJunJulAugSepOctNovDec' : ".*$month" / 3`
8


Hein.