Reputation: 131
I have a problem and I dont know why my code doesn't work
the code:
prot="abc"
awk '{
if ( $1 == $prot )
print $2
}' file.txt
but when I change my code to this it works as it was intented to work
awk '{
if ( $1 == "abc" )
print $2
}' file.txt
Why is that happening?
Upvotes: 1
Views: 334
Reputation: 41446
Here is an other variation:
prot="abc"
awk '{
if ( $1 == prot )
print $2
}' prot="$prot" file.txt
Upvotes: 0
Reputation: 289505
You cannot use a bash variable directly into your script.
Instead, give it with the -v
option:
-v prot="$prot"
All together:
prot="abc"
awk -v prot="$prot" '{
if ( $1 == prot )
print $2
}' file.txt
Upvotes: 2