Need help in formatting the date taken from a variable

Hi,

I am having the below data in input file. The file contains multiple such lines.
The file is comma delimited.

AAA,M,CCCCCC,EE,DD,FF,GG,1187.00000,01-MAY-05
BBB,M,CCCCCC,EE,DD,FF,GG,87.00000,10-MAY-05

I need to create below output file out of it-

<tag1>AAA</tag1>
<date>2005-05-01</date>
<tag1>BBBB</tag1>
<date>2005-05-10</date>

So far i had done below but i need help on formating the date.

awk 'BEGIN { FS=","; } { printf "<tag1>%s</tag1>\n<date>%s</date>\n", $1,$9}' inputfile

Regards,
Arjun

How about

awk '
BEGIN           {for (n=split ("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", T); n>0; n--) M[T[n]] = n
                 FS=","
                }
                {split ($9, T, "-")
                 printf "<tag1>%s</tag1>\n<date>20%02d-%02d-%02d</date>\n", $1, T[3], M[T[2]], T[1]
                }
' file
<tag1>AAA</tag1>
<date>2005-05-01</date>
<tag1>BBB</tag1>
<date>2005-05-10</date>

Hello Arjun_CV,

Welcome to forums, please use code tags as per forum rules. For your problem there are following things which I am considering and providing this code.

i- Your month names will be JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC style always into your Input_file.(Else you could have change it according to your requirement too.)
ii- Output which you have shown to us as <tag1>BBBB</tag1> , where BBBB is a typo. As I could see only BBB into shown input.
Following may help you in same.

awk -F, '{split($NF, array,"-");num=split("JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC", months," ");for(i=1;i<=num;i++){if(months==array[2]){Q=sprintf("%02d", i)}};print "<tag1>" $1 "</tag1>" ORS "<date>" "20" array[3] "-" Q "-" array[1] "</date>"}'   Input_file

Output will be as follows.

<tag1>AAA</tag1>
<date>2005-05-01</date>
<tag1>BBB</tag1>
<date>2005-05-10</date>

EDIT: Adding a non-one liner form for solution too now.

 awk -F, '{
                split($NF, array,"-");
                num=split("JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC", months," ");
                for(i=1;i<=num;i++){
                                        if(months==array[2]){
                                                                Q=sprintf("%02d", i)
                                                               }
                                   };
                print "<tag1>" $1 "</tag1>" ORS "<date>" "20" array[3] "-" Q "-" array[1] "</date>"
          }
         '   Input_file
 

Thanks,
R. Singh

Or even

awk -F, '
                {split ($9, T, "-")
                 printf "<tag1>%s</tag1>\n<date>20%02d-%02d-%02d</date>\n", $1, T[3], (index ("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", T[2]) + 3) / 4, T[1]    
                }
' file
1 Like