Little explanation needed on array

I had gone through..google search.....and unix user post.......where I found so many ways of accessing files..... suppose if I am having 4 files, each file is having 3 columns, and I want to use each field of each column, then how can I use it.. how can I create array for each file's each column, and How do I access each field in C we usually use loops to access data,but C is very lenghty, AWK seems to be simple.. please anyone explain array for handling multiple file

Please mention the aim of doing what you are asking, with input and output samples.

HI Dona...you can do something like this if you want to use multiple files
awk '{
          {getline <"file1"; a[NR]=$1;b[NR]=$2;c[NR]=$3} # a,b,c array of File1 column1,column2,column3 
          {getline <"file2"; d[NR]=$1;e[NR]=$2;f[NR]=$3}
          {getline <"file3"; g[NR]=$1;h[NR]=$2;i[NR]=$3}
          {getline <"file4;   j[NR]=$1;k[NR]=$2;l[NR]=$3 }
          }
END{for(i=1;i<=some  value;i++) print a,b,c........}' OFS=" \t" # if you want to  print you can use loop and OFS is tab separated here.

in above example I shown printing of array, you can do you some other operation also.

1 Like
awk 'FNR==1{i++} {for(k=1; k<=NF; k++) A[i,FNR,k]=$k} END{print ".."}' file[1-4]

Would create create a pseudo 3-dimensional array A that can be processed in the END section...

For example: A[2,6,3] will contain the 6th row, 3rd column of the 2nd file.

1 Like

Scrutinizer will you please explain #4 with an example...I think I can learn something from you. In fact, I got one problem with nested loop with array only..in following post.
kindly Scrutinizer help me in this post I used your multidimension array method..

Thank you Scrutinizer and Akshay.. Scrutinizer's method is effiecient I feel..when there is multiple files, multiple fields...

Scrutinizer I wanted to know how to copy 3 dimensional array please give me one example

whether

for(i=1;i<=25;i++)
{
for(m=1;m<=NF;m++)
A[i,m]=A[1,i,m]
}

this is a way to copy or any other ? please reply..

The easiest way would be for(X in A) B[X]=A[X]

This works because awk, techically, doesn't have three-dimensional arrays. When you do ARR[A,B,C], it jams A, B, and C together into a single string like A SUBSEP B SUBSEP C.

So you can still loop through them all in a single loop. If you want the individual components of X, you can split them apart on SUBSEP ( a special variable which defaults to an odd, nonprinting ASCII character)

Thank you, in my case I think I have to use loop because after each calculation I am storing result in new field

for example

for(i=1;i<=10;i++)
# Dummy calculation
x=A[1,i,5]+...........A[1,i,7]
B[1,i,1]=x
 {
for(m=1;m<=NF;m++)
B[1,i,m+1]=A[1,i,m]
}