1833762 Members
2768 Online
110063 Solutions
New Discussion

Re: Date conversion

 
SOLVED
Go to solution
Mike_Ca Li
Regular Advisor

Date conversion

HI :
I have eg:
01Jan04
24Jun04
how to convert to
01-Jan-04
24-Jun-04 ?
Thanks
9 REPLIES 9
Michael Tully
Honored Contributor

Re: Date conversion

Try this:

date %d%b%y
Anyone for a Mutiny ?
Patrick Wallek
Honored Contributor

Re: Date conversion

#!/usr/bin/sh
DATE=$1
DAY=$(echo ${DATE} | cut -c 1-2)
MONTH=$(echo ${DATE} | cut -c 3-5)
YEAR=$(echo ${DATE} | cut -c 6-7)

echo "${DAY}-${MONTH}-${YEAR}"


Invoke it as:

# conv_date 01Jan04
And the output should be:
01-Jan-04
Michael Schulte zur Sur
Honored Contributor
Solution

Re: Date conversion

Hi,

like
echo "01Jan04" | awk '{print substr($1,1,2)"-"substr($1,3,3)"-"substr($1,6,2)}'

hth,

Michael
Michael Tully
Honored Contributor

Re: Date conversion

obviously I didn't read the question correctly ... sorry
Anyone for a Mutiny ?
Rodney Hills
Honored Contributor

Re: Date conversion

How about this perl one liner-

perl -p -e 's/(..)(...)(..)/$1-$2-$3/' inpfile >outfile

HTH

-- Rod Hills
There be dragons...
Hein van den Heuvel
Honored Contributor

Re: Date conversion



perl -pe 's/(\D+)/-$1-/' infile > outfile

Now this of course will only work with the very simple examples you show.

Just for grins,

Hein.
H.Merijn Brand (procura
Honored Contributor

Re: Date conversion

And for *all* other formats (including those of your example):

# perl -wMDate::Manip -e'$dt=UnixDate($string,"%d-%b-%Y");

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Date conversion

And for *all* other formats (including those of your example):

# perl -wMDate::Manip -e'$dt=UnixDate($string,"%d-%b-%Y")'

where $string is your date
if the dates are in a file

# perl -wMDate::Manip -pe'$_=UnixDate($_,"%d-%b-%Y")' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Saravanan_11
Occasional Advisor

Re: Date conversion

hai ,

Use in the following form date "+%d-%h-%y"

# date "+%d-%h-%y"
29-Jul-04
#

-Saravanan