In short, this is an alphabetical comparison for GNU awk
.
Because we compare a string ('203-344-1234'
) with a number ('40'
).
What is the comparison type
From [GNU awk] String Type versus Numeric Type:
When two operands are compared, either string comparison or numeric
comparison may be used. This depends upon the attributes of the
operands, according to the following symmetric matrix:
+---------------------------------------------------------
| STRING NUMERIC STRNUM
+---------------------------------------------------------
| STRING | string string string
| NUMERIC | string numeric numeric
| STRNUM | string numeric numeric
+---------------------------------------------------------
How a number is converted to string
From [GNU awk] Comparison Operators:
When comparing operands of mixed types, numeric operands are
converted to strings using the value of CONVFMT
(see section
Conversion of Strings and Numbers).
Following the link above, we see how the number is converted to a string for the comparison. A string is created with a call to sprintf()
and the awk predefined variable CONVFMT
is used for formatting, default it is "%.6g"
, which would keep at most 6 significant digits for a decimal. But for an integer:
As a special case, if a number is an integer, then the result of
converting it to a string is always an integer, no matter what the value of CONVFMT
may be.
For this case, $4
is 203-344-1234
, is a string, not a number. And the second argument is a number, converted to the string '40'
(regardless of the CONVFMT
value, because it is an integer).
How to force string or number
Some additional information, from the same page:
If, for some reason, you need to force a number to be converted to a
string, concatenate that number with the empty string, ""
. To force a
string to be converted to a number, add zero to that string
Sometimes, to ensure that a field will be used as numeric, we write it like $1+0
. For example, in this case, if we force $4+0
it will be an integer with the first three digits, 203
(that's it, the attempt to create a number ended at the first non-numeric character).
tab
delimited file?