AWK: Discrepancy in numeric output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: Discrepancy in numeric output
# 1  
Old 08-27-2009
AWK: Discrepancy in numeric output

During a file-system cleanup I noticed a strange behavior of awk (HP-UX 11iv3 / IA64). When summing up the size of files in one directory it gives different numbers when using print as opposed to printf:
Code:
find . -type f -name '*.dmp.Z' -mtime +35 -exec ls -l {} \+ | \
awk 'BEGIN{ OFMT="%f" } { total += $5 } END{ printf "%d\n", total; print total }'
1439122395
5734089691.000000

However, if I use floating point conversion for printf, the numbers are equal. Is there any (possibly documented) reason for this behavior? For this example I can adjust easily, but it might break older scripts who's coders never were aware of this.

Last edited by pludi; 08-27-2009 at 09:28 AM.. Reason: Overly long code-line
# 2  
Old 08-27-2009
I've had trouble in shell with integers equal to or greater than 2147483648 (1024 * 1024 * 1024 * 2).

Code:
This is ok.
expr 2147483646 + 1
2147483647

This is wrong!
expr 2147483647 + 1
-2147483648

This is ok.
echo "2147483647 + 1"|bc
2147483648


Code:
Your numbers !
expr 5734089690 + 1
1439122395


Code:
The limit is mentioned in:
man 5 limits

INT_MAX     2147483647


Last edited by methyl; 08-27-2009 at 09:51 AM.. Reason: Added big numbers overflow example
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search of multiple numeric entries in an output file

Hello Tech Guys, I have two files named check.txt and output.txt Content of check.txt 620070527336551 40201800027285 620070551928314 40201800027285 620070534376312 40201800027285 620070536668046 02711306140261 620070248491123 02711306140261 620070553851296 02711306140261... (4 Replies)
Discussion started by: Xtreme
4 Replies

2. Shell Programming and Scripting

awk output discrepancy

I noticed a discrepancy while running AWK on different platforms/versions: SunOS $ echo "78" | awk '{ printf "%c\n", $0 }' N $ awk 'BEGIN{ printf "%c\n", "78" }' /dev/null N Linux / HP-UX $ echo "78" | awk '{ printf "%c\n", $0 }' N $ awk 'BEGIN{ printf "%c\n", "78" }' /dev/null 7 Can... (4 Replies)
Discussion started by: Yoda
4 Replies

3. Shell Programming and Scripting

filtering a numeric value which has '%' using awk

Hello Gurus, I have a requirement where I have to filter a value from some field which has 99% or greater than '99%'.. For ex: The Date (file -- sample.csv) will be like below Field1,Field2,Field3,Field4 860440512,844284992,16155520,99% 860440512,844284992,16155520,94%... (4 Replies)
Discussion started by: raghu.iv85
4 Replies

4. Shell Programming and Scripting

Storing or Using Numeric Output From a Function

Hi all, I'm trying to implement a linear congruential pseudorandom number generator (<http://en.wikipedia.org/wiki/Linear_congruential_generator>), since $RANDOM and /dev/random aren't standardized. I'm referring to the Shell & Utilities volume of POSIX.1-2008, but I'm running into some odd... (3 Replies)
Discussion started by: PehJota
3 Replies

5. Shell Programming and Scripting

Extract resultset numeric value from isql output ?

isql output comes as below, (0 rows affected) (1 row affected) (7 rows affected) How to extract the resultset number alone from the particular line ?? such as 0 1 7 (3 Replies)
Discussion started by: vikram3.r
3 Replies

6. Shell Programming and Scripting

Awk , Sed Print last 4 numeric characters

Hello All, I have been searching and trying this for a bit now. Can use some assistance. Large 5000 line flat file. bash, rhel5 Input File Sinppet: Fri Oct 30 09:24:02 EDT 2009 -- 1030 Fri Oct 30 09:26:01 EDT 2009 -- 73 Fri Oct 30 09:28:01 EDT 2009 -- 1220 Fri Oct 30 09:30:01 EDT... (9 Replies)
Discussion started by: abacus
9 Replies

7. Shell Programming and Scripting

Check for Numeric output in Perl

Hi All, I would like to convert my below csh script to Perl. Can any expert help ? # To check for numeric input set tested1 = `echo "$tested"| awk '/^+$/'`; # To remove un-neccessary zeros set tested2 = `echo "$tested"|awk '{print $0+0}'`; (3 Replies)
Discussion started by: Raynon
3 Replies

8. Shell Programming and Scripting

Conversion of Exponential to numeric in awk- not correct output

Hi All, I have 1 million records file. Using awk, I am counting the number of records. But as the number is huge, after crossing a number, awk is displaying it in exponential format. At the end, I need to verify this count given by awk with expected count. But as it is in exponential format,... (3 Replies)
Discussion started by: ssunda6
3 Replies

9. UNIX for Dummies Questions & Answers

assigning (numeric) command output to var tcsh

Hello, I'm trying to assign a numeric value that is returned from one of my programs to a variable in tcsh. I want to do: @ r10 = './my_prog file 35' where ./my_prog file 35 returns a decimal value, but this doesn't work. How do I achieve the desired result? Janet (4 Replies)
Discussion started by: psran
4 Replies

10. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies
Login or Register to Ask a Question