Frage

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?

War es hilfreich?

Lösung

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

Andere Tipps

Here is an other variation:

prot="abc"
awk '{
if ( $1 == prot )
     print $2
}' prot="$prot" file.txt
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top