awk comparing variables

Is there a way to compare variables in a 'awk'?
I've been trying for a while and can't figure it out. I'm guessing its not possible :confused:

VAR=Bob
awk '$3 == $VAR { print $1 }' file.txt

Regards
Jikuu

VAR=Bob
awk -v VAR=$VAR  '$3 == VAR { print $1 }' file.txt

This is the more modern way, POSIX awk.

1 Like

and if i wanted multiple variables?

VAR1=Bob
VAR2=Steve

awk -v VAR1=$VAR1 VAR2=$VAR2 '($3 == VAR1 && $4 == VAR2) { print $1 }' file.txt

I know the '($3 == VAR1 && $4 == VAR2)' bit is right but I'm not too sure on declaring multiple variables. What separator would be used?

awk -v VAR1=$VAR1 -v VAR2=$VAR2 '($3 == VAR1 && $4 == VAR2) { print $1 }' file.txt
1 Like

Thanks for the help. Much appreciated.