1

I have requirement where first "n" rows ("n" being variable passed at run time) should be stored in "n" corresponding arrays, array name being named as arrayRow"NR"; here, NR is the built-in awk variable.

awk -F"," -v n=$Header_Count 'NR<=n{
split($0,**("arrayRow" NR)**) }' SampleText.txt

So the first row should be stored in array arrayRow1; the second row should be stored in arrayRow2, ... etc. Any ideas on how to create dynamic array name in awk?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116

1 Answers1

2

You can't do it without a hacky shell loop and bad coding practices for letting shell variables expand to become part of an awk command, or a function that populates hard-coded, predefined arrays given a numeric arg or similar. Don't even try it.

If you have or can get GNU awk you should use true multi-dimensional arrays instead:

$ cat file
a b c
d e
f g h
i j k
l m

$ awk -v n=3 '
NR<=n {arrayRow[NR][1]; split($0,arrayRow[NR]) }
END {
    for (i in arrayRow)  {
        for (j in arrayRow[i]) {
            print i, j, arrayRow[i][j]
        }
    }
}
' file
1 1 a
1 2 b
1 3 c
2 1 d
2 2 e
3 1 f
3 2 g
3 3 h

Otherwise you can use pseudo-multi-dimensional arrays in any awk:

$ awk -v n=3 '
NR<=n { for (i=1;i<=NF;i++) arrayRow[NR,i] = $i; nf[NR]=NF }
END {
    for (i in nf) {
        for (j=1; j<=nf[i]; j++) {
            print i, j, arrayRow[i,j]
        }
    }
}
' file

but you don't actually need multi-dimensional arrays for this at all, just use 1-dimensional and split the string into a new array when you need it:

$ awk -v n=3 '
NR<=n {arrayRow[NR] = $0}
END {
    for (i in arrayRow)  {
        split(arrayRow[i],arrayRowI)
        for (j in arrayRowI) {
            print i, j, arrayRowI[j]
        }
    }
}
' file
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Ed Morton
  • 188,023
  • 17
  • 78
  • 185