Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing long integers in awk

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
like image 928
jaypal singh Avatar asked Jan 13 '12 22:01

jaypal singh


People also ask

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output.

What is awk '{ print $3 }'?

If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.

Which is faster awk or cut?

awk is more powerfull than cut. if you need to use tail or head or sort or similars, and cut, you can make one single awk for that. Like other posters said, if you can use cut for you problem you should choose it instead of awk, but there are situations where cut just isn't enough.


1 Answers

This answer was partially answered by @Mark Wilkins and @Dennis Williamson already but I found out the largest 64-bit integer that can be handled without losing precision is 2^53. Eg awk's reference page http://www.gnu.org/software/gawk/manual/gawk.html#Integer-Programming

(sorry if my answer is too old. Figured I'd still share for the next person before they spend too much time on this like I did)

like image 69
3150 Avatar answered Sep 21 '22 21:09

3150