1

In general string comparison, "A" > "a" is false.

However, I am getting unexpected result from this awk execution:

$ echo "A a"| awk '{if ($1 > $2) print "gt"; else print "leq"}'
gt

What am I missing?

Environment info:

$ uname -r -s -v -M
AIX 1 6 IBM,9110-510

$ locale
LANG=en_AU.8859-15
LC_COLLATE="en_AU.8859-15"
LC_CTYPE="en_AU.8859-15"
LC_MONETARY="en_AU.8859-15"
LC_NUMERIC="en_AU.8859-15"
LC_TIME="en_AU.8859-15"
LC_MESSAGES="en_AU.8859-15"
LC_ALL=

Diagnostics:

$ echo "A a"| awk '{print NF}'
2

Update It produces the correct result after setting LC_ALL=POSIX (thanks JS웃). Need to investigate further into this.

4

1 回答 1

1

我无法重现这一点,但您可以通过将操作数与空字符串连接来强制进行字符串比较:

echo "A a"| awk '{if ($1"" > $2"") print "gt"; else print "leq"}'

注意: 与任何一个操作数连接就足够了。

更新:

怀疑locale是OP的设置导致了这个问题。设置后LC_ALL=POSIX问题解决。

于 2013-07-02T22:59:32.420 回答