Using Awk to Add Numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Awk to Add Numbers
# 1  
Old 06-10-2010
Using Awk to Add Numbers

Code:
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 the above actually adds up the list of numbers?
# 2  
Old 06-10-2010
Something like this?
Code:
echo "0.1 2.0 0.4 2.0 4.3 1.0 6.0 9.0" | awk -v RS=" " '{total += $1} END {print total}'

# 3  
Old 06-10-2010
Code:
$ echo "0.1 2.0 0.4 2.0 4.3 1.0 6.0 9.0" | awk 'BEGIN {total=0}{for(i=1;i<=NF;++i){total+=$i}} END {printf "%.2f\n",total}'
24.80

# 4  
Old 06-10-2010
Thank you guys. This works!
# 5  
Old 06-14-2010
i think I spoke too soon with this one. it appears the numbers aren't adding up.

i was using the below code:

Code:
awk 'BEGIN {total=0}{for(i=1;i<=NF;++i){total+=$i}} END {printf "%.2f\n",total}'

but it looks like its not adding up the numbers correctly.

i should mention that the numbers with the echo quotes are expected to change. meaning, there can be 5, 10, 400 numbers that needs to be added up.

Code:
echo "5.3 3.4 43.5 etc" | awk 'BEGIN {total=0}{for(i=1;i<=NF;++i){total+=$i}} END {printf "%.2f\n",total}'



---------- Post updated at 01:14 PM ---------- Previous update was at 01:01 PM ----------

please ignore this. my mistake. it works. typo in the script i was working on. thanks again guys.
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. 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

3. Shell Programming and Scripting

Add numbers in a file

Hello, How to add numbers that are read from a file /tmp/test The content of the file look like 1234 234 432 1235 123 I read the file content in a for loop f=/tmp/test for i in `cat $f` do . . done Santhosh (11 Replies)
Discussion started by: mvsanthosh
11 Replies

4. Shell Programming and Scripting

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. grep '^L' $FileName| awk -F"|" ' {... (7 Replies)
Discussion started by: gsjdrr
7 Replies

5. UNIX for Dummies Questions & Answers

how can i add two numbers

hi, i am having one file which looks like the one below: ABC1 *** 1 4 ABC2 *** 7 12 ABC3 *** 0 34 ... (4 Replies)
Discussion started by: kripssmart
4 Replies

6. Shell Programming and Scripting

How to add numbers

Hello. I new to Shell Scripting. I have a file and here is the output of the file. 1.1M 1.1M 3.3M 149K 61K 75K 144K 135K 82K 170K 327K 2.0M 219K 165K (8 Replies)
Discussion started by: email-lalit
8 Replies

7. Shell Programming and Scripting

Add a list of numbers

I need to add a list of numbers contained in a file. For example, the file would look like this: 10 290 342 5409 I need to get a total sum of all the numbers in the list. Any ideas? Thanks! (2 Replies)
Discussion started by: TheCrunge
2 Replies

8. Shell Programming and Scripting

how do you to add numbers incrementally?

I've refined the filesystem size using awk and directed to a file name. eg, here's the content in a file called "numbers" $cat numbers 345 543 23423456 44435 546 . . how do you write a script to all these numbers to get the total? thanks a lot. (9 Replies)
Discussion started by: kiem
9 Replies

9. Shell Programming and Scripting

How to add numbers?

:confused: I have plain text file "tmp" which include a range of numbers(bytes), say like: 123 234 567 2434 2323 213123 How can I add them and display out. should I use AWK, then how? I am a newer in Bourne shell, please give me a hand, thanks a lot (7 Replies)
Discussion started by: pnxi
7 Replies

10. Shell Programming and Scripting

Add some numbers!

im using this command to return the number of links in my directory, grep -c -i -h "href" *html */*html *htm *shtml is there a way of adding these to get the total? Cheers (3 Replies)
Discussion started by: TalkShowHost
3 Replies
Login or Register to Ask a Question