shell - passing for loop index into awk -


i trying pass loop index awk keep getting unexpected token awk errors.

first tried using -v option within awk:

for in "${myarray}" awk -v var=$i '/var/{print}' myfile.dat done 

i tried calling variable directly using single quotes:

for in "${myarray}" awk '/'"$i"'/{print}' myfile.dat done 

my end goal learn how pass loop index variable through awk search pattern. i'd above code search through myfile.dat , print lines contain strings in myarray.

there 2 problems:

  1. array traversing should for in "${myarray[@]}"
  2. awk treats text between /.../ regex literal, use variable use $0 ~ var.

your code should be:

for in "${myarray[@]}";    awk -v var="$i" '$0 ~ var' myfile.dat done 

{print} default action in awk can omit shown above.


Comments