Not getting array in .awk file and print it

I have test.sh file as below :

set -A IDARR $ID
echo | awk -f test.awk -v TempArr="${IDARR
[*]}"

I have test.awk file as below :

BEGIN {
Flag = 1;
}
{
 print "Hello";
 for(i in TempArr)
 {
  print i;
 }
}

Here script is running fine when I run like test.sh
but not getting output as
Hello and array values....

Please help me what is wrong in the script

TempArr is just a variable inside awk, its not an array as you think and trying to iterate through for (i in arr) construct.

What does your $ID contains? May be you can get the array created inside awk itself.

1 Like

You need to split the TempArr variable into an awk Array, which then you can iterate through.

1 Like

As RudiC said you have to use split function

Here is example to start

$ bashArray=(10 20 30 4 50)
$ cat test.awk 
BEGIN{ 
          split(variable,awkArray); 
          for(i in awkArray)
          print "index="i,"Element="awkArray
     }
$ awk -f test.awk -v variable="${bashArray[*]}"

Resulting

index=4 Element=4
index=5 Element=50
index=1 Element=10
index=2 Element=20
index=3 Element=30
1 Like

Thanks Akshay,

Whne I tried as below code, no is giving the 0 value and for that resion not getting any value from array. Please help me on this... :slight_smile:
OUTPUT like :
0
After spliting

but not getting array elements...

        no = split(TempArr, Temp);
        print no;
        print "After spliting";
        for(i in Temp)
        {
                print Temp;
        }
 

Please show your full code. if TempArr is awk variable, and not empty then it should work, before using split , put this print TempArr and check, whether variable is empty or not, if its prints nothing then what you are getting "0" is right. since variable has nothing to split.

When I tried to print that values in test.sh file, I am getting 2 values from array

set -A IDARR $ID
echo "${IDARR
[*]}"
echo | awk -f test.awk -v TempArr="${IDARR
[*]}"

Getting out put as below :
3 1

Thanks in advance... :slight_smile:

Yes, and what's wrong? What would you expect? Show us the contents of test.awk and IDARR.

Here I am getting output from test.sh file not from test.awk file
I wanted that array values from test.sh to test.awk file

Use this

$ cat test.ksh
#!/bin/ksh

# some value
ID="1 2 3 4 5 6 7"

# Array
set -A IDARR $ID

# Echo array contents...
echo "From shell ${IDARR[*]}"

# Execute awk script
awk -f test.awk -v TempArr="${IDARR[*]}"
$ cat test.awk
BEGIN {

 # this is variable
 print "from awk", TempArr;

 # split variable contents into array with default FS space
 split(TempArr,NewArray)

 # print contents of NewArray we created above..
 for(i in NewArray)
 {
  print "index",i,"element",NewArray;
 }
}

Resulting

$ ksh test.ksh 
From shell 1 2 3 4 5 6 7
from awk 1 2 3 4 5 6 7
index 4 element 4
index 5 element 5
index 6 element 6
index 7 element 7
index 1 element 1
index 2 element 2
index 3 element 3

From terminal if you want to execute awk script try like this

$ awk -f test.awk -v TempArr="Hai hello world this is test"

Resulting

from awk Hai hello world this is test
index 4 element this
index 5 element is
index 6 element test
index 1 element Hai
index 2 element hello
index 3 element world