Utku
Utku

Reputation: 2177

awk reports 0 for an empty field, whereas comparison reports negative for it

progfile:

$3 < 0 { printf("3rd field is negative. It is: %d\n", $3) }
$3 == 0 { printf("3rd field is zero. It is: %d\n", $3) }
$3 > 0 { printf("3rd field is positive. It is: %d\n", $3) }

input_file:

field1 field2

command:

awk -f progfile input_file

output:

3rd field is negative. It is: 0

What's going on?

Upvotes: 0

Views: 48

Answers (1)

karakfa
karakfa

Reputation: 67507

what you're testing is whether empty string is less than 0. Equivalent to this

$ awk 'BEGIN{print ""<0}'
1

the conversion to 0 is due to printf "%d". If you print it will be still empty string.

Upvotes: 1

Related Questions