Mawk printf %d maxes out at 2147483647


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Mawk printf %d maxes out at 2147483647
# 1  
Old 01-10-2014
Mawk printf %d maxes out at 2147483647

So, I do some file processing that generates very large numbers, such as total amount GETted from a busy web cluster in a month, etc. Mawk is awesome-- fast and easy. It's awk! But, there's a fatal flaw that I'd like to overcome. Apparently, %d maxes out at 2147483647. Here's sample output, with the first line sprintf'd and the second just print'd:

Code:
total_size, total_count, average:       2147483647 50586 2493242
total_size, total_count, average:       1.26123e+11 50586 2.49324e+06

Is there any way I'm not thinking of to achieve the same result as %d? I'm using the very latest, bright and shiny mawk:

Code:
# mawk -W version
mawk 1.3.4 20131226

There are bug reports out there about this (google "mawk 2147483647") but no solutions so far. May thanks in advance, and please pardon me if I've overlooked some solution. I was a bit fatigued when I went looking.
# 2  
Old 01-10-2014
Try using "gawk".
# 3  
Old 01-10-2014
Quote:
Originally Posted by bartus11
Try using "gawk".
That is a solution, of course, but I'm using mawk because of the size of the datasets we're working with. Depending on the task, we have anywhere from 3 to 7 times better performance. So, it's been an ongoing project to convert those tasks that we can to mawk. I'd like to do so for this one as well, as it is a very time-consuming job.
# 4  
Old 01-10-2014
Try this then:
Code:
printf "%.0f\n", 2147483648

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 01-10-2014
Quote:
Originally Posted by bartus11
Try this then:
Code:
printf "%.0f\n", 2147483648

Ah, of course. That works. :-) Don't know why I assumed that would also be broken. Many thanks.
# 6  
Old 01-10-2014
I've dug into mawk's code a bit and switching it to a 64-bit integer isn't quite as easy as it seems. It's a sticky problem, because of the mutability of numbers in awk. They are quite careful to get a 32-bit int and a 64-bit double, since all 32-bit integers can be faithfully represented by a 64-bit float, but what happens when your int is 64-bit? Not all 64-bit integers can be perfectly represented by the 53-bits precision of a 64-bit float.

It also passes on its printf options into the system printf's, almost completely faithfully, except for a weird case they added in 1995 for a system that only had 16-bit ints. I suspect another such weird case would be needed for 64-bits.

Last edited by Corona688; 01-10-2014 at 06:49 PM..
# 7  
Old 01-10-2014
In fact I'd go far enough to say... The mawk developers might do better, in readability and performance, to write their own printf. Cooperating with every awkward printf of the last 20 years has made it very strange internally.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

[SOLVED] Making mktime/strftime available to mawk

I frequently use awk time functions and am switching some scripts over to mawk. I don't have the mktime or strftime functions in mawk, but it appears that there is a way, as explained here in "Time functions": Please only cut-and-past links to man pages from our man pages. So, simple... (10 Replies)
Discussion started by: treesloth
10 Replies

2. Shell Programming and Scripting

MAWK does not support length(array)?

As Brendan O'Conner writes in this blog, mawk is near 8 times faster than gawk, so I am going to give mawk a go, but I got errors when trying to print the length of an array in mawk using length() function, is it not supported in mawk? or there's another way to get the length of an array in mawk? ... (3 Replies)
Discussion started by: kevintse
3 Replies

3. UNIX for Dummies Questions & Answers

The meaning of %s in printf

I have this command like that has %s in it, I know %s calls a column, but I am not sure I understand which column (I mean for my case I can check the input file, but I want to know how is this %s used, how comes tha same symbo; gives different columns in one command line: {printf "grep %s... (22 Replies)
Discussion started by: cosmologist
22 Replies

4. UNIX for Dummies Questions & Answers

mawk script to compare 2 files and report where they match

I have two files and would like a report of where they match. Example of file1: 1 1 1 2 2 2 13 14 15 4 4 4 15 16 17 100 102 1004 56 57 890 Example of file2: 2 2 2 16 10 11 45 22 35 13 14 15 1001 1002 3456 100 102 1004 (1 Reply)
Discussion started by: kenneth.mcbride
1 Replies

5. UNIX for Dummies Questions & Answers

Need help with printf

Hi, I have just completed my first script (:D) and now i just need to format it with printf. This is what I have: #!/bin/ksh TOTB=0 TOTF=0 TOTI=0 HOST=`hostname` echo " FSYSTEM BLKS FREE INUSE MOUNTEDON" df -m | grep -v ":"|grep -v Free|grep -v "/proc"| while read FSYSTEM... (2 Replies)
Discussion started by: compan023
2 Replies

6. Shell Programming and Scripting

IF and awk/printf

Hi Friends, Scripting newb here. So I'm trying to create a geektool script that uses awk and printf to output certain fields from top (namely command, cpu%, rsize, pid and time, in that order). After much trial and error, I've pretty much succeeded, with one exception. Any process whose name... (3 Replies)
Discussion started by: thom.mattson
3 Replies

7. Shell Programming and Scripting

awk and printf

echo $bbsize 1.5 echo $fillpercent .95 echo $bbsize | awk '{printf "%.2f\n",$0*$fillpercent}' 2.25 echo $bbsize | awk '{printf "%.2f\n",$0*.95}' 1.42 1.42 is what I'm expecting... echo $blocksize 4096 echo $bbsize | awk '{printf "%.2f\n",$0*$blocksize}' 2.25 echo $bbsize |... (3 Replies)
Discussion started by: xgringo
3 Replies

8. Shell Programming and Scripting

printf in awk

Hi friends.. I am confused about awk printf option.. I have a comma separated file 88562848,21-JAN-08,2741079, -1188,-7433,TESTING 88558314,21-JAN-08,2741189, -1273,-7976,TESTING and there is a line in my script ( written by someone else) What is the use of command? I guess... (10 Replies)
Discussion started by: clx
10 Replies

9. Shell Programming and Scripting

printf

How to print output in following format? A..................ok AA................ok AAA..............ok AAAAAA........ok "ok" one under one (4 Replies)
Discussion started by: mirusnet
4 Replies

10. Programming

printf

What is the output of the following program considering an x86 based parameter passing sequence where stack grows towards lower memory addresses and that arguments are evaluated from right to left: int i=10; int f1() { static int i = 15; printf("f1:%d ", i); return i--; } main() {... (2 Replies)
Discussion started by: arunviswanath
2 Replies
Login or Register to Ask a Question