Date Format

Hello friends,

I have one question regarding the change of date format since I am new to shell scripting please help me by giving your precious time ....

Q: I want to change the format of date from 1-Nov-2011 and change this format into 1-11-2011 please help me regarding this . It will be a great help for the people like me who is new to this field.

Your support will be appreciated.

I have used this

echo 1-Nov-2011 | sed "s/\(.\{4\}\)\(..\)\(..\)\(..\)\(..\)\(..\)/\3-\2-\1 \4:\5:\6/"

Thanks

Try this...

date -d "1-Nov-2011" '+%d-%m-%Y'

--ahamed

Hi ahamed,

Thanx a lot friend for helping me it is a command can you tell me the proper scipt because i don't know it will be a great help.

---------- Post updated at 07:59 AM ---------- Previous update was at 07:39 AM ----------

Hi ahamed,

It is showing date -d and it is showing -d as illegal operation.

echo "1-Nov-2011" | awk -F"-" 'BEGIN{ a["Jan"]=1;a["Feb"]=2;a["Nov"]=11;a["Dec"]=12 }{ print $1 OFS a[$2] OFS $3 }' OFS="-"

Of course, fill the remaining months...

If Solaris, use nawk

--ahamed

A Shell way: Adjust according to whether you wanted a leading zero on the month number or not.

ddmmmyy="1-Nov-2011"
dd=`echo "${ddmmmyy}"|cut -f1 -d\-`
mmm=`echo "${ddmmmyy}"|cut -f2 -d\-`
yy=`echo "${ddmmmyy}"|cut -f3 -d\-`
echo "dd=${dd}  mmm=${mmm}  yy=${yy}"
#
case "${mmm}" in
        "Jan")  mm="1" ;;
        "Feb")  mm="2" ;;
        "Mar")  mm="3" ;;
        "Apr")  mm="4" ;;
        "May")  mm="5" ;;
        "Jun")  mm="6" ;;
        "Jul")  mm="7" ;;
        "Aug")  mm="8" ;;
        "Sep")  mm="9" ;;
        "Oct")  mm="10" ;;
        "Nov")  mm="11" ;;
        "Dec")  mm="12" ;;
esac
echo "dd=${dd}  mm=${mm}  yy=${yy}"
echo "${dd}-${mm}-${yy}"

./scriptname
dd=1  mmm=Nov  yy=2011
dd=1  mm=11  yy=2011
1-11-2011
date '+%d-%m-%Y'
$ echo "1-Nov-2011" | awk -F- 'BEGIN{a="  JanFebMarAprMayJunJulAugSepOctNovDec"}{printf("%s-%s-%s",$1,index(a,$2)/3,$3)}'
1-11-2011

Nice!...

--ahamed