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

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

问题描述

我有一个以竖线分隔的提要文件,其中包含多个字段.由于我只需要几个,我想使用 awk 来捕获它们以用于我的测试目的.但是,我注意到如果我使用 "%d"printf 会更改该值.如果我使用 "%s",它工作正常.

Feed 文件示例:

<块引用>

[jaypal:~/Temp] cat temp

302610004125074|19769904399993903|30|15|2012-01-13 17:20:02.346000|2012-01-13 17:20:0003|19769904399993903|30|15|2012-01-17|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||ARG|

我有兴趣捕获第二列,即19769904399993903.

这是我的测试:

[jaypal:~/Temp] awk -F"|";'{printf ("%d
",$2)}' 温度19769904399993904 # 值改变了

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

[jaypal:~/Temp] awk -F"|";'{printf ("%s
",$2)}' 温度19769904399993903 # 值保持不变[jaypal:~/Temp] awk -F"|";'{print $2}' 温度19769904399993903 # 值保持不变

所以这是%d"无法处理长整数的限制.如果是这样,为什么它会在数字上加一而不是截断它?

我已经用 BSDGNU 版本的 awk 尝试过这个代码>.

版本信息:

[jaypal:~/Temp] gawk --versionGNU awk 4.0.0版权所有 (C) 1989, 1991-2011 自由软件基金会.[jaypal:~/Temp] awk --versionawk 版本 20070501

解决方案

我相信这种情况下的底层数字格式是 IEEE 双精度型.所以改变的值是浮点精度错误的结果.如果确实有必要将大值视为数字并保持准确的精度,则最好使用具有处理任意精度算术能力(可能通过扩展)的 Perl、Ruby 或 Python 之类的工具.

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
",$2)}' temp
19769904399993904   # Value is changed

However, the following two tests works fine -

[jaypal:~/Temp] awk -F"|" '{printf ("%s
",$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天全站免登陆