在打印AWK长整数 [英] Printing long integers in awk

查看:463
本文介绍了在打印AWK长整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪几种字段管道分隔饲料文件。因为我只需要几个,我想用 AWK 来捕捉他们对我的测试目的。然而,我注意到, 的printf 如果我使用的变化值 %D 。如果我使用 %的正常工作

饲料文件示例:


  

[jaypal:〜/温度]猫温度


  
  

302610004125074 | 19769904399993903 | 30 | 15 | 2012-01-13 17:20:02.346000 | 2012-01-13 17:20:03.307000|E072AE4B|587244|316|13|GSM|1|SUCC|0|1|255|2|2|0|213|2|0|6|0|0|0|0|0|10|16473840051|30|302610|235|250|0|7|0|0|0|0|0|10|54320058002|906|722310|2|0||0|BELL MOBILITY蜂窝,INC |贝尔移动蜂窝,INC |贝尔移动| AMX ARGENTINA SA |。克拉罗又名CTI Movil公司| CAN | ARG |


我感兴趣的是捕获的 第二列 19769904399993903

这是我的测试:

  [jaypal:〜/温度] awk的-F| '{printf的(%d个\\ n,$ 2)}'温度
19769904399993904#值被改变

但是,下面的两个测试工作正常 -

  [jaypal:〜/温度] awk的-F| '{printf的(%S \\ n,$ 2)}'温度
19769904399993903#值保持相同[jaypal:〜/温度] awk的-F| {打印$ 2}'温度
19769904399993903#值保持相同

这就是 %D的限制无法处理长整数。如果多数民众赞成的情况下,为什么要添加一个到数而不是可能会截断呢?

我是 BSD GNU 的版本 AWK

版本信息:

  [jaypal:〜/ Temp文件夹] GAWK --version
GNU awk的4.0.0
版权所有(C)1989,1991-2011自由软件基金会。[jaypal:〜/温度] AWK --version
AWK版20070501


解决方案

我相信,在这种情况下,潜在的数字格式是IEEE双。因此,改变的值是浮点precision错误的结果。如果它实际上是必要的治疗大值作为数字和保持准确precision,它可能会更好使用的东西如Perl和Ruby或Python它有能力(也许通过扩展)来处理arbitrary- precision算术。

I have a pipe delimited feed file which has several fields. Since I only need a few, I thought of using awk to capture them for my testing purposes. However, I noticed that printf changes the value if I use "%d". It works fine if I use "%s".

Feed File Sample:

[jaypal:~/Temp] cat temp

302610004125074|19769904399993903|30|15|2012-01-13 17:20:02.346000|2012-01-13 17:20:03.307000|E072AE4B|587244|316|13|GSM|1|SUCC|0|1|255|2|2|0|213|2|0|6|0|0|0|0|0|10|16473840051|30|302610|235|250|0|7|0|0|0|0|0|10|54320058002|906|722310|2|0||0|BELL MOBILITY CELLULAR, INC|BELL MOBILITY CELLULAR, INC|Bell Mobility|AMX ARGENTINA SA.|Claro aka CTI Movil|CAN|ARG|

I am interested in capturing the second column which is 19769904399993903.

Here are my tests:

[jaypal:~/Temp] awk -F"|" '{printf ("%d\n",$2)}' temp
19769904399993904   # Value is changed

However, the following two tests works fine -

[jaypal:~/Temp] awk -F"|" '{printf ("%s\n",$2)}' temp
19769904399993903   # Value remains same

[jaypal:~/Temp] awk -F"|" '{print $2}' temp
19769904399993903   # Value remains same

So is this a limit of "%d" of not able to handle long integers. If thats the case why would it add one to the number instead of may be truncating it?

I have tried this with BSD and GNU versions of awk.

Version Info:

[jaypal:~/Temp] gawk --version
GNU Awk 4.0.0
Copyright (C) 1989, 1991-2011 Free Software Foundation.

[jaypal:~/Temp] awk --version
awk version 20070501

解决方案

I believe the underlying numeric format in this case is an IEEE double. So the changed value is a result of floating point precision errors. If it is actually necessary to treat the large values as numerics and to maintain accurate precision, it might be better to use something like Perl, Ruby, or Python which have the capabilities (maybe via extensions) to handle arbitrary-precision arithmetic.

这篇关于在打印AWK长整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆