user3597432
user3597432

Reputation: 131

Unix script using awk

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

Answers (2)

Jotne
Jotne

Reputation: 41446

Here is an other variation:

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

Upvotes: 0

fedorqui
fedorqui

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

Related Questions