欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

gawk中的字符串数字(STRNUM)介绍

程序员文章站 2022-06-01 12:57:53
这篇文章主要介绍了gawk中的字符串数字(STRNUM)介绍,需要的朋友可以参考下... 16-12-04...

几天前在论坛看到一个帖子 这是个挺有意思的问题。
我自己做了几个试验,发现00e1有这个问题,但是00a1、00b1、00c1等其它字符串却没有这个问题。

前天,怀着忐忑的心情给 bug-gawk@gnu.org 发了一封邮件:

[bug report] gawk doesn’t work correctly when assign special value to variable
to: bug-gawk@gnu.org

1. symptom:
gawk doesn’s work correctly when assign special value (like 00e1) variable.

2. steps to repeat this issue:

1) download and compile the latest gawk 4.1.3
http://ftp.gnu.org/gnu/gawk/
[root]# gawk --version | head -2
gnu awk 4.1.3, api: 1.1
copyright (c) 1989, 1991-2015 free software foundation.

2) prepare a test file
cat > 1.txt << eof
00e1 00e1
00e2 00e4
00e3 00e1_01
eof

3) execute following command to see the result
[root]# gawk -v var="00e1" '$2==var' 1.txt
00e1 00e1
00e2 00e4

the second line should not be there. because "00e4" is not equal to "00e1" obviously.
is it a bug or by design?

3. workaround:
gawk -v var="00e1" '$2==""var' 1.txt
gawk -v var="^00e1___fckpd___0quot; '$2~var' 1.txt

没想到,昨天就收到了来自 andrew j. schorr 大神的回复,效率还真是高呢,32个赞:

on thu, aug 06, 2015 at 03:55:50pm +0800, shell_hat wrote:
> the second line should not be there. because "00e4" is not equal to "00e1" obviously.
> is it a bug or by design?

this is a tricky area. please check the manual for the discussion of "string type versus numeric type":
http://www.gnu.org/software/gawk/manual/html_node/variable-typing.html

i think the command-line assignment results in a variable of type strnum.

> 3. workaround:
> gawk -v var="00e1" '$2==""var' 1.txt
> gawk -v var="^00e1___fckpd___1quot; '$2~var' 1.txt

these examples may also be helpful:

bash-4.2$ gawk 'begin {var = "00e1"} $2 == var' 1.txt
00e1 00e1
bash-4.2$ gawk 'begin {var = 00e1} $2 == var' 1.txt
00e1 00e1
00e2 00e4

regards,
andy

看完那篇文档,似有所悟。但是还是没搞清楚为啥00a1、00b1、00c1等字符串没问题呢?
喝完一杯咖啡之后,恍然大悟:科学计数法!泥煤的~~~