×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

COnvert Month into a numeric value in Solaris

COnvert Month into a numeric value in Solaris

COnvert Month into a numeric value in Solaris

(OP)
Hi
   I am looking for a short and neat program to convert Month Name like Sep to 09 in Solaris

I do not see any date function that can help. This can be for any month and not necessarily current month

I have tried Case statement it works but very messsy.

Input provided
Sep 09

Output expected
09 09

Thanks for your help in advance
Naveen
Replies continue below

Recommended for you

RE: COnvert Month into a numeric value in Solaris

sun [721560]-> date '+%m %d'     
10 03
sun [721561]-> date         
Mon Oct  3 12:02:43 MDT 2011

# for help:

sun [721562]-> man date

     example% date '+DATE: %m/%d/%y%nTIME:%H:%M:%S'

     generates as output

     DATE: 08/01/76

     TIME: 14:45:05

     Example 2: Setting the Current Time

     The following command sets the current time to 12:34:56:

     example# date 1234.56
 

A great teacher, does not provide answers, but methods to teach others "How and where to find the answers"

bsh

37 years Bell, AT&T, Lucent, Avaya
Tier 3 for 27 years and counting
http://bshtele.com

RE: COnvert Month into a numeric value in Solaris

This is pretty simple to code...

CODE

#!/bin/ksh

month_no()
{
        typeset -u MON=$1

        case $MON in
                JAN)    MNO=01;;
                FEB)    MNO=02;;
                MAR)    MNO=03;;
                APR)    MNO=04;;
                MAY)    MNO=05;;
                JUN)    MNO=06;;
                JUL)    MNO=07;;
                AUG)    MNO=08;;
                SEP)    MNO=09;;
                OCT)    MNO=10;;
                NOV)    MNO=11;;
                DEC)    MNO=12;;
                *)      MNO=XX;;
        esac

        print $MNO
}

month_no Jan
month_no jul
month_no SEP
month_no moocow

DT="Aug 20"

print "Original Date:  ${DT}"

MO=${DT%% *}
DY=${DT##* }
DT="$(month_no ${MO}) ${DY}"

print "Converted Date: ${DT}"
 

RE: COnvert Month into a numeric value in Solaris

(OP)
Thanks Sambones and Avaya

I have this code currently .I am looking at an alternative like a one or few lines of code  with Awk ..etc

Cheers
Naveen  

RE: COnvert Month into a numeric value in Solaris

Hmmm, is this a school assignment?

Whenever someone turns down a working solution because it's not a specific method, it's often because it's a homework assignment.

 

RE: COnvert Month into a numeric value in Solaris

(OP)
SamBones
   No. Its not honestly. I am looking for a alternative as i could not figure one .

I am using this as input to a touch command which create a file dd .Based on that start timestamp i have  to find files that has changed in a given interval.There can be multiple logfiles.It works fine with  this date format (10/6/11 20:15 10/6/11 23:15)

but when i give Oct 6 20:15 Oct 6 23:15 you can see i have to write such a long Proram to convert Month to number so Touch can understand

Hope i have justified

Input given to program
10/6/11 20:15 10/6/11 23:15
===================program snippet ===================
touchdate=$3
touchtime=$4

echo " date is $touchdate"
echo "Time is $touchtime"

echo "touch -t "$3""$4" $HOME/dd"
touch -t "$3""$4" $HOME/dd

find . -type f -newer  $HOME/dd |awk -F'/' '{print $2}'

 

RE: COnvert Month into a numeric value in Solaris

That doesn't seem to be the correct date/time format for a "touch" command. I believe it wants it in this format...

CODE

[[CC]YY]MMDDhhmm [.SS]
So for your example you should be passing it "1110062015", or just "10062015".

See the man page for "touch" for more info.

 

RE: COnvert Month into a numeric value in Solaris

(OP)
Sambones
       Touch command works for the specified Date formatwith the format i have in the script .And also for the Month (I have same program as yours)

I have no issues reported by touch (bad command argument ..etc)

Anyway i think i am still looking for a shortcut.I will post once i figure out

Cheers
naveen

RE: COnvert Month into a numeric value in Solaris

CODE

(echo Sep 09) | \
awk 'BEGIN { month["Jan"]="01" ; month["Feb"]="02" ; month["Mar"]="03" ; month["Apr"]="04" ; month["May"]="05" ; month["Jun"]="06" ; month["Jul"]="07" ; month["Aug"]="08" ; month["Sep"]="09" ; month["Oct"]="10" ; month["Nov"]="11" ; month["Dec"]="12" } { print month[$1], $2 }'

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close