[Solved] Two dates comparision in AIX

Hi all,

I have a shell script which connects to database and gets two date values in two variables a and b.

When I do echo a, echo b, it shows dates such as

a=15-JUN-13
b=14-APR-13

Now, I want to say if (a > b) send email but I get error bad number. Looks like these dates are not in the correct format and hence I get an error.

How can I accomplish this task on AIX?

Thanks in Adv.

  • Sam

Take a look at this thread: Date Arithmetic in FAQ section.

Hi, I went thru almost all the posts but could not find answer to my question. I am sorry if I missed any post that has it.

Could someone please help?

Thanks!

Ok, here is one way of doing:

#!/bin/ksh

a="15-JUN-13"
b="14-APR-13"

awk -v A="$a" -v B="$b" '
        BEGIN {
                j = split("JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC",M,",")
                split(A,m,"-")
                split(B,n,"-")
                for ( i = 1; i <= j; i++ )
                {
                        if(M==m[2])
                                D1=m[1] i m[3]
                        if(M==n[2])
                                D2=n[1] i n[3]
                }
                if(D1>D2)
                        print A " is greater"
                else
                        print B " is greater"
        }
'

Yoda, posted solution doesn't work for
12-MAR-13 and 14-DEC-03
or
2-JUN-13 and 14-APR-13

To correct, you could use the following replacement for the D1 and D2 assignments:

a="12-MAR-13"
b="14-DEC-03"

awk -v A="$a" -v B="$b" '
BEGIN {
        j = split("JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC",M,",")
        split(A,m,"-")
        split(B,n,"-")
        for ( i = 1; i <= j; i++ )
        {
                if(M==m[2])
                        D1=sprintf("%02d%02d%04d", m[3], m[1], i)
                if(M==n[2])
                        D2=sprintf("%02d%02d%04d", n[3], n[1], i)
        }
        if(D1>D2)
                print A " is greater"
        else
                print B " is greater"
}'
1 Like

Thanks to both of you for a very quick help. That saved my day.