Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing array as command line argument

Status
Not open for further replies.

MGoel

Programmer
Jan 26, 2002
17
US
Hi All,

I have let's say an array fil_array = elem1 elem2 ..

so what i want is invoke a awk script passing this array from shell script to the awk code.

How can i do and then later access in the awk code.

Thanks in advance

Manoj



 
gawk and nawk support the v option to import shell variables.
ex: gawk -v var=$myvar ' {printf "%s\n", var}'

However this only works with scalars,not arrays.
You would have to use something like:
function processArray() {
myarray[1]=1
myarray[2]=1
myarray[3]=1

for x in "${[myarray[@]}"
do
awk -v var=${myarray[$x]} ' {printf "%s\n",var}' filename
done
}

Good Luck
 
Thanks marsd.

I knew about the -v feature and was using it till i hit the limitation of command line args length. So that's when i thought mebbe i can use an array.

But now that you confirmed it is not possible I had to use couple of args one time and that I moved some code to run another awk script with the left over args.

Thanks for your help

Manoj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top