The issue here is that you are trying to get shell variable expansion inside single quotes ' ... '
. However, inside single quotes, shell variable expansion is suspended (see e.g. this answer here or Lhunath & GreyCat's Bash Guide), which is the reason why this is actually recommended: as the $
performs a similar function in awk
in dereferencing individual input fields (but where the field number can be expressed by a variable name as well, as in e.g. $NF
), enclosing the program in single quotes avoids concurrent "variable expansions".
In your case, you can "import" the value into the awk
program with
awk -v fieldnr="$i" '{if ($NF==fieldnr) {print $NF}}'
Still, it would appear that your problem can be solved entirely in awk
, so maybe you want to explain what you want to accomplish in more detail and we can try to find a more elegant (and possibly faster) way; it may be reasonable to open another question, though ...
print n
toprint n+0
then you don't need the-v n=0
up front. – Ed Morton Feb 18 '20 at 18:11