awk 'BEGIN { print 3 > 2 }' a.out
Prints nothing
But
awk 'BEGIN { print (3 > 2) }' a.out
> 1
Prints 1. This repros with any file. Why do the parenthesis have an effect?
GNU AWK 5.1
awk has Redirection like the shell. Check your current directory for a file named "2" -- it will have one line containing the value "3".
With parentheses, you're changing how awk parses the code: giving the expression 3 > 2
higher priority, you're forcing awk to use the relational operator.
This is discussed in the Precedence chapter of the manual:
< <= == != > >= >> | |&
Relational and redirection. The relational operators and the redirections have the same precedence level. Characters such as '>' serve both as relationals and as redirections; the context distinguishes between the two meanings.
See the index of the gawk manual for all uses of >
that's why, whenever i need x greater-than y
compares in awk
, the safer approach is reverse it and write
y < x
that way, only getline
ones need to be watched out for, as opposed to accidentally overwriting files
'{ print x > y }'
that happen to have the same name as y
's string-value. side note - to best of my understanding, the cleanest way to force numeric compare is probably
+y < +x # doing ( 0+y < 0+x ) is superfluous
# in this context since unary `+` has an
# implicit `0` to left of it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.