How to deal with multidimensional array in awk?

Hi all!

I would like to know how to print

$0

when using multidimensional array like below

time being I am using for loop to print columns like this

awk 'FNR==1{i++}
{for(k=1;k<=NF;k++)A[i,FNR,k]=$k}
END{for(j=1;j<=25;j++)
print A[2,1,1],A[2,1,2],A[2,j,3],A[2,j,4],A[2,j,1],A[2,j,2],A[2,j,3],A[2,j,4],A[1,j,1],A[1,j,1],A[1,j,2],A[1,j,3],A[1,j,4],A[1,j,1]}' file1 file2 

so here my problem is I have more than 15 column that means whether I have to type like below till 15th column

A[2,j,1],A[2,j,2],.....A[2,j,15]

or any other solution is available ?

thanks in advance

I'm not sure your print A[2,1,1],A[2,1,2], . . . , A[2,j,1], . . . is intended as is or just sloppy typing. While I don't really understand the pattern of your above print statement, I've dreamed up following which might come close to what you need:

END {for (i=2; i>=1; i--) for (j=1; j<=25; j++) for (k=1; k>=4; k++) printf "%s%s", A[i,j,k], OFS; printf "\n"} 

EDIT: at a second look I see you need 25 lines with the strange pattern given above; in that case, you need to permute the loops so the middle index is the first to run, add a newline printf at its end, and then loop the others...

1 Like

Thank you so much Sir, your solution worked fine for me, Sir please tell me how can I put NF of file 1 in counter like LC[i]=NR for line count you suggested in some other post

NF or NR?

FNR==1 {LC = LNR}         # save last iteration's record no. in LC array
       {LNR = NR}            # keep this iteration's record no. for use in next it.

Sir I need

NF

to calculate number of columns in file

OK, then try to adapt above proposal and post!