AWK Array problem

Dear All,

I am facing problem to get right output through awk program

I have file in which �B� value is appearing multiple time and I need to capture all these values.

My script is

BEGIN { FS=" " }
{
 
        if ( substr($1,1,5) == "START" )             
        {
        i = i+1;
        SFILENAME = FILENAME
        }
        if ( $1 == "input" )
                INPUT = $2
        if ( $1 == "output" )
                OUTPUT = $2
                if ( $1 == "B" && k < i )
        {
                k = k+1;
                B[k] = $2
        }
        if ( $2 == "input_type" )
                INPUT_TYPE = $3
        if ( $2 == "output_type" )
                OUTPUT_TYPE = $3
        if ( $1 == "B" )
                                B_2 = $2
 
}
 
END{ for (j=1 ; j <=i ; j++  )
 
printf "%s,%s,%s,%s,%s,%s,\n",SFILENAME[j],INPUT[j],OUTPUT[j],B[kh],INPUT_TYPE[j],OUTPUT_TYPE[j],B_2[j]
}

And sample file is

START
input AAAA
output AAAA.output
B 567
B extra
F input_type xxv
F output_type xxvoo
B 333
.
.
F input_type xxd
B arv
.
START
input BBBB
output BBBB.output
B 666
F input_type xxv
F output_type xxvoo
.
.
F input_type xxd
B arv
.
 
START
input BBBB
output BBBB.output
B 666
F input_type xxv
F output_type xxvoo
.
.
F input_type xxd
B arv
.
.
START
input NNNN
output NNNN.output
B 54
B extra
F input_type xxv
F output_type xxvoo
B 656
.
.
F input_type xxd

Getting output like this

awk -f my_pro.awk file.txt
file.txt,AAAA,AAAA.output,,xxd,xxvoo,
file.txt,BBBB,BBBB.output,,xxd,xxvoo,
file.txt,BBBB,BBBB.output,,xxd,xxvoo,
file.txt,NNNN,NNNN.output,,xxd,xxvoo,
 

required output is

file.txt,AAAA,AAAA.output, 567, extra,xxd,xxvoo, arv
file.txt,BBBB,BBBB.output, 666,xxd,xxvoo, arv
file.txt,BBBB,BBBB.output, 54,xxd,xxvoo, 656
file.txt,NNNN,NNNN.output,,xxd,xxvoo,

Edit by pludi: please use code tags, and please don't change the font and size for each line individually. It's possible to do this for the whole text, which improves rendering.

use below modification:-

BEGIN { FS=" " }
{
 
        if ( substr($1,1,5) == "START" )             
        {
        i = i+1;
        SFILENAME = FILENAME
        }
        if ( $1 == "input" )
                INPUT = $2
        if ( $1 == "output" )
                OUTPUT = $2
                if ( $1 == "B"  )
        {
                
                B = B","$2 ;
        }
        if ( $2 == "input_type" )
                INPUT_TYPE = $3
        if ( $2 == "output_type" )
                OUTPUT_TYPE = $3
}
 
END{ for (j=1 ; j <=i+1 ; j++  )
{

n=split(B[j],A,",");  a=A[n] ; for(i=1;i<n;i++){C[j]=C[j]A"," } ;
printf "%s,%s,%s %s,%s,%s,%s\n",SFILENAME[j],INPUT[j],OUTPUT[j],C[j],INPUT_TYPE[j],OUTPUT_TYPE[j],a

}
}

:D:D:D

Great!!!

Thanks..