0

Below is the code:

for i in "${x[@]}"
do
        z+=$(awk '$1 == '$i' {print $2}' $file)
done
echo "${z[@]}"

It is giving following error with desire output:

awk: cmd. line:1: $1 ==  {print $2}
awk: cmd. line:1:        ^ syntax error

I am guessing that when $1 value is not matching with $i value it is showing this error.

Can someone tell me how this can be handle?

I tried with -v:

z+=$(awk -v '$1 == '$i' {print $2}' $file)

but it is giving fatal:

awk: fatal: `$1 ' is not a legal variable name
2
  • you're sure the x array doesn't have an empty value in it? (x=(""))
    – Jeff Schaller
    Commented Nov 8, 2017 at 14:29
  • x array doesn't have an empty value in it.
    – serenesat
    Commented Nov 8, 2017 at 14:33

1 Answer 1

1

Pass $i to awk as a variable and so:

z+=$(awk -v vari="$i" '$1 == vari {print $2}' "$file")
1
  • It hanged, no output.
    – serenesat
    Commented Nov 8, 2017 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .