Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COnvert Month into a numeric value in Solaris

Status
Not open for further replies.

naveenrt

Technical User
Mar 23, 2001
53
0
0
US
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
 
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
 
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}"
 
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
 
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.

 
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}'

 
That doesn't seem to be the correct date/time format for a "[tt]touch[/tt]" 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 "[tt]touch[/tt]" for more info.

 
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
 
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 }'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top