add signed and unsigned numbers- awk help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add signed and unsigned numbers- awk help
# 1  
Old 06-10-2010
add signed and unsigned numbers- awk help

Hi All,

I have written the below to add the numbers in a column. Postive numbers are unsigned and negative numbers are signed in the file. After the below cmd I am getting -0.00 , instead of 0.00. Can someone guide me on what I am missing in the cmd.

Code:
 
grep '^L' $FileName| awk -F"|" ' { tot+=$20 } END { printf("%.2f",tot) } '|read lntotal

Thanks!
# 2  
Old 06-10-2010
Hi,

First the use of grep is not necessary. You can include it in the awk block. For your sign problem, it may come from a precision error. Can you provide a sample file that generate that error?
# 3  
Old 06-10-2010
Thanks ripat. I agree with you, but I am not a great expert in awk and I tend to go to awk only if I can't achieve in other way :-(.

Following is the subset of data from file, the entire file looks something similar. All the positive amounts on top and negative in the bottom(file balances all the time,so sum should be 0.00). If I add the below I am getting the correct num. I don't know what is going wrong with the full file. I am on SunSolaris.

L|1|0|cp|18|60||1|CO||US|US||0||0||ABC ||8.05|0||||||||||
L|1|0|cp|18|60||1|CO||US|US||0||0||ABC ||21.35|0||||||||||
L|1|0|cp|68|92||1|CO||US|US||0||0||ABC ||-8.05|0||||||||||
L|1|0|cp|68|92||1|CO||US|US||0||0||ABC ||-21.35|0||||||||||
# 4  
Old 06-10-2010
No problem here with the sign:
Code:
$ cat f
L|1|0|cp|18|60||1|CO||US|US||0||0||ABC ||8.05|0|||||||||| 
L|1|0|cp|18|60||1|CO||US|US||0||0||ABC ||21.35|0|||||||||| 
L|1|0|cp|68|92||1|CO||US|US||0||0||ABC ||-8.05|0||||||||||
L|1|0|cp|68|92||1|CO||US|US||0||0||ABC ||-21.35|0||||||||||

$awk -F"|" '/^L/{tot+=$20}END{printf "%.2f",tot}' f

> 0.00

# 5  
Old 06-10-2010
Still I see the same result.

Code:
 
awk -F"|" '/^L/{tot+=$20} END { printf "%.2f",tot }' TEM1.TXT
-0.00
 
Data--
 
awk -F"|" '/^L/{print $20}' TEM1.TXT
8.05
21.35
13.68
23.34
18.71
98.95
411.61
0.63
185.58
0.52
18.03
7.71
65.22
22.49
38.57
33.60
9.70
3.11
25.78
634.95
0.06
1.15
40.99
70.64
0.54
0.16
2.78
71.03
83.76
367.16
0.09
42.17
0.71
8.28
1.27
1.36
0.06
10.70
3.27
34.11
0.06
0.05
2.87
11.22
0.03
2.90
6.73
-0.63
-0.52
-0.06
-0.54
-0.16
-0.09
-0.71
-0.06
-0.06
-0.05
-0.03
-1.15
-1.27
-1.36
-2.78
-2.87
-2.90
-3.11
-3.27
-6.73
-7.71
-8.28
-8.05
-9.70
-10.70
-11.22
-13.68
-18.03
-18.71
-21.35
-22.49
-23.34
-25.78
-33.60
-34.11
-38.57
-40.99
-42.17
-65.22
-70.64
-71.03
-83.76
-98.95
-185.58
-367.16
-411.61
-634.95

# 6  
Old 06-10-2010
Try with nawk or /usr/xpg4/bin/awk.
# 7  
Old 06-10-2010
Nope, not working. Tried both and got the same result.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Efficient awk way to add numbers in line fields

data.now: blah1,dah,blaha,sweet,games.log,5297484456,nagios-toin,529748456,on__host=93 SERVICE__ALERT_=51 Warning___The__results__of__service=16 Warning___on__host=92 Auto_save__of__retention__data__completed=1 Warning___Return=68 PASSIVE__SERVICE__CHECK_=53 ,1026--1313,1... (12 Replies)
Discussion started by: SkySmart
12 Replies

2. OS X (Apple)

Unsigned to signed, error?...

Hi guys... Macbook Pro, 13", circa August 2012, OSX 10.7.5, default bash terminal. I require the capability to convert +32767 to -32768 into signed hex words... The example piece code below works perfectly except... #/bin/bash # sign.sh # Unsign to sign... while true do # I have used... (2 Replies)
Discussion started by: wisecracker
2 Replies

3. UNIX for Dummies Questions & Answers

[Solved] awk solution to add sequential numbers based on a word

Hi experts, I've been struggling to format a large genetic dataset. It's complicated to explain so I'll simply post example input/output $cat input.txt ID GENE pos start end blah1 coolgene 1 3 5 blah2 coolgene 1 4 6 blah3 coolgene 1 4 ... (4 Replies)
Discussion started by: torchij
4 Replies

4. Programming

Signed and unsigned intergers

when a date type is considered signed and unsigned is that simple referring to - for signed and positive numbers for unsigned? Further if that is the case would mutiplying and dividing ect where 2 signed numbers, like (-2)*(-2) = 4 result in a unsigned. (3 Replies)
Discussion started by: Fingerz
3 Replies

5. Programming

[ASM] Adding SIGNED numbers?

Hi guys, I want to add a list of SIGNED numbers... but I don't know how to tell the computer to ADD THEM as signed, let me explain further: when adding 200 + (-100) , it becomes 100, but in asm the computer always add them as unsigned, so I always get the 300. Do I have to add them in a... (4 Replies)
Discussion started by: lamachejo
4 Replies

6. Programming

Help with understanding ( int, char, long, short, signed, unsigned etc.... )

My question is simple: When should I use a long, int, char, unsigned/signed variables?? When I declare a variable "unsigned;" what did I do it??? Why would I delcare an integer "long" or "short" ( unsigned or signed)?? Any examples of when things like "unsigned", "long", "short" etc...... (6 Replies)
Discussion started by: cpp_beginner
6 Replies

7. UNIX for Advanced & Expert Users

"Signed Linux" - Only executing signed programs

Hey folks, not sure whether this or the security board is the right forum. If I failed, please move :) So here's the problem: I need to build a Linux environment in which only "signed" processes are allowed to run. When I say signed I don't mean a VeriSign signature like you know it from... (5 Replies)
Discussion started by: disaster
5 Replies

8. Shell Programming and Scripting

Using Awk to Add Numbers

echo "0.1 2.0 0.4 2.0 4.3 1.0 6.0 9.0" | awk 'BEGIN {total=0} {total += $1} END {print total}' I want to add the above output from the echo command, but i can't figure this out. The output above always spits out inaccurate numbers. can someone please provide me with a one liner similar to... (4 Replies)
Discussion started by: SkySmart
4 Replies

9. Programming

to get the correct value with unsigned int

hi, Please help me with the following code to get the difference in values. struct a{ int b1; int c1; char d1; } main() { unsigned int b=10; unsigned int c; c = b - (unsigned int )sizeof(a); printf("%d",c); } Here c returns some junk value. How can i get the... (2 Replies)
Discussion started by: naan
2 Replies

10. Programming

Unsigned int

How can I store and/or print() a number that is larger than 4 294 967 295 in C? is int64_t or u_int64_t what I need ? if, so how can I printf it to stdout? (2 Replies)
Discussion started by: nimnod
2 Replies
Login or Register to Ask a Question