noobCoder
noobCoder

Reputation: 73

why does my awk code that should print word only on specific condition actually prints for all lines?

I am trying to learn awk functionalities and as a simple exercise I try to print values in a file where if the first word is PERMNO then it should print the third word else it should just ignore.

the code I am using is

awk '{if ($1 = "PERMNO"){ print $3}}' ddoutput.txt 

Right now this prints third word from every line. But I expect it to print only the third word when the first word of line is PERMNO. What am I missing?

Upvotes: 2

Views: 67

Answers (1)

oguz ismail
oguz ismail

Reputation: 50750

With $1 = "PERMNO" you're assigning PERMNO to first field, which always evaluates to true. You should use the == operator like:

awk '{if($1=="PERMNO"){print $3}}' file

Or more awkish:

awk '$1=="PERMNO"{print $3}' file

Upvotes: 3

Related Questions