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
VAR=Bob
awk '$3 == $VAR { print $1 }' file.txt
Regards
Jikuu
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
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.
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
Thanks for the help. Much appreciated.