Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347612
  • 博文数量: 38
  • 博客积分: 1289
  • 博客等级: 中尉
  • 技术积分: 533
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-10 22:30
个人简介

Good Good Study. Day Day Up.

文章分类

全部博文(38)

文章存档

2018年(1)

2016年(3)

2015年(12)

2014年(7)

2013年(10)

2012年(5)

分类: LINUX

2015-08-07 15:15:46

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

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

点击(此处)折叠或打开

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

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


  1. 2. Steps to repeat this issue:


  1. 1) Download and compile the latest gawk 4.1.3
  2. [root]# gawk --version | head -2
  3. GNU Awk 4.1.3, API: 1.1
  4. Copyright (C) 1989, 1991-2015 Free Software Foundation.


  1. 2) Prepare a test file
  2. cat > 1.txt << EOF
  3. 00e1 00e1
  4. 00e2 00e4
  5. 00e3 00e1_01
  6. EOF


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


  1. The second line should not be there. Because "00e4" is NOT equal to "00e1" obviously.
  2. Is it a bug or by design?


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

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

点击(此处)折叠或打开

  1. On Thu, Aug 06, 2015 at 03:55:50PM +0800, Shell_HAT wrote:
  2. > The second line should not be there. Because "00e4" is NOT equal to "00e1" obviously.
  3. > Is it a bug or by design?


  1. This is a tricky area. Please check the manual for the discussion of "String Type versus Numeric Type":


  1. I think the command-line assignment results in a variable of type STRNUM.


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


  1. These examples may also be helpful:


  1. bash-4.2$ gawk 'BEGIN {var = "00e1"} $2 == var' 1.txt
  2. 00e1 00e1
  3. bash-4.2$ gawk 'BEGIN {var = 00e1} $2 == var' 1.txt
  4. 00e1 00e1
  5. 00e2 00e4


  1. Regards,
  2. Andy

看完那篇文档,似有所悟。但是还是没搞清楚为啥00a1、00b1、00c1等字符串没问题呢?
喝完一杯咖啡之后,恍然大悟:科学计数法!泥煤的~~~
阅读(1468) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~