RNB
RNB

Reputation: 99

Awk act as a substitution

I have a list of arrays with different information on which I am doing an awk.

number  location  length  value

 1        2         40     0.96
---       5         45     0.97
 4        5         47     0.96
---       5         35     0.95
 2        5         60     0.95
---       3         55     0.96


awk '{if ($2=5 && $3 >= 40 && $3<=50 && $6>=0.96) print $0}' Infile.txt

It does give me the correct row --- 5 45 0.97 and 4 5 47 0.96. However, if I want to add another condition, such as $1= ---, to only have the first output --- 5 45 0.97

 awk '{if ($2=5 && $3 >= 40 && $3<=50 && $6>=0.96 && $1="\-\-\-") print $0}' Infile.txt >List_position.txt

it acts as a substitution, returning the previous output as 1 5 45 0.97 and 1 5 47 0.96. I tried with $1=---, $1='\-\-\-' and they both didn't work. If I try with $1="---", it substitutes $1 to ---.

I am new with awk and I really don't understand why it does a substitution. If " " is a substitution in awk, how can I put a condition on ---?

Upvotes: 0

Views: 65

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754570

You've done an assignment = instead of comparison == (and the result of the assignment evaluates to true because it is neither 0 nor an empty string).

awk '{if ($2==5 && $3 >= 40 && $3<=50 && $6>=0.96 && $1=="---") print }' Infile.txt >List_position.txt

You also have the '= vs ==' problem with the $2=5 assignment in the condition, but you didn't notice it because you were expecting to see 5 there (and thanks to JS웃 for pointing that out).

You also don't need the backslashes.

Upvotes: 3

Related Questions