1

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?

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

+1.. but please fix the all together code block.. :-)
I get improper assignment -v prot="$prot"{..
0

Here is an other variation:

prot="abc"
awk '{
if ( $1 == prot )
     print $2
}' prot="$prot" file.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.