# AWK Array problem

**URL:** <https://community.unix.com/t/awk-array-problem/263298>\
**Category:** Shell Programming and Scripting\
**Created:** [April 7, 2010, 3:27am UTC](https://community.unix.com/t/awk-array-problem/263298 "2010-04-07T03:27:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvindng](https://community.unix.com/letter_avatar/arvindng/32/5_5575768a8748004e209b776fc1b2916d.png) [@arvindng](https://community.unix.com/u/arvindng)\
**Post date:** [April 7, 2010, 3:27am UTC](https://community.unix.com/t/awk-array-problem/263298/1 "2010-04-07T03:27:56Z")

</div>

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

```nohighlight
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

```nohighlight
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

```nohighlight
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

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![ahmad.diab](https://community.unix.com/user_avatar/community.unix.com/ahmad.diab/32/940_2.png) [@ahmad.diab](https://community.unix.com/u/ahmad.diab)\
**Post date:** [April 7, 2010, 6:21am UTC](https://community.unix.com/t/awk-array-problem/263298/2 "2010-04-07T06:21:04Z")

</div>

use below modification:-

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![arvindng](https://community.unix.com/letter_avatar/arvindng/32/5_5575768a8748004e209b776fc1b2916d.png) [@arvindng](https://community.unix.com/u/arvindng)\
**Post date:** [April 7, 2010, 9:11am UTC](https://community.unix.com/t/awk-array-problem/263298/3 "2010-04-07T09:11:54Z")

</div>

Great!!!

Thanks..
