Substitute string with an index number

Objective is to substitute Jan with 01, Feb with 02 and so on. The month will be provided as input.

I could construct below awk and it worked.

echo Jun | \
awk 'BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",mon," ")}{ for (i=1;i<=12;i++){ if ($1==mon) printf("%02d\n",i)} }'

Wanted to know if there are better alternatives

$ date -j -f "%b" "Jun" "+%m"
06

@rajamadhavan - Did not work. I'm in AIX.

AIX doest seems to have the format conversion options..

may need to do something like this..

echo "Jan" | awk 'BEGIN{a["Jan"]="01";a["Feb"]="02"}{print a[$0]}'
$ echo Jun | awk ' { printf("%02d",int(index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)/3)+1) } '
06
2 Likes