Adding row of numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding row of numbers
# 8  
Old 06-12-2012
perl can do it

Code:
perl -e 'print 15314.6 + 16320.6 + 15813.6 + 16835.6 + 19252.1 + 14823.5 + 16577.1 + 15068.1 + 13631 + 18158.1 + 17889.6 + 16835.6 + 15314.6 + 15314.6 + 16320.6 + 17358.6 + 15563.1 + 18158.1 + 18158.1 + 16835.6 + 17889.6 + 16577.1 + 253294000 + 17889.6 + 19252.1 + 17623.1 + 18975.6 + 12266 + 19530.6 + 18158.1 + 18158.1 + 17096.1 + 17623.1 + 19530.6 + 19252.1 + 21536.1 + 18428.6 + 19811.1 + 17096.1 + 19811.1 + 18428.6 + 18975.6 + 18158.1 + 18975.6 + 17096.1 + 19530.6 + 18158.1 + 16577.1 + 18701.1 + 16577.1 + 19811.1 + 16066.1 + 18158.1 + 14340.5 + 17623.1 + 17358.6 + 18701.1 + 16577.1 + 16320.6 + 18975.6 + 19252.1 + 16320.6 + 13398.5 + 18975.6 + 15563.1 + 17889.6 + 16835.6 + 14102 + 11183.5 + 18158.1 + 18158.1 + 17623.1 + 17623.1 + 14581 + 16835.6 + 16066.1 + 17889.6 + 14340.5 + 17889.6 + 18158.1 + 17889.6 + 16066.1 + 18701.1 + 20664.6 + 20093.6 + 18158.1 + 21830.7 + 17623.1 + 20093.6 + 17358.6 + 19811.1 + 19530.6 + 19530.6 + 18975.6 + 18975.6 + 19530.6 + 19252.1 + 20953.1 + 20378.1 + 17889.6 + 17889.6 + 17358.6 + 19252.1 + 18975.6 + 18158.1 + 16835.6 + 18158.1 + 18975.6 + 14340.5 + 16835.6 + 15813.6 + 19811.1 + 17096.1 + 17889.6 + 14102 + 15813.6 + 19252.1 + 18158.1 + 16577.1 + 20093.6 + 20378.1'
255411071.499999

This User Gave Thanks to Leion For This Post:
# 9  
Old 06-13-2012
the perl solution seems to work. does anyone know how to use awk for this?
# 10  
Old 06-13-2012
Example:

Code:
$ echo ""|awk '{a=15314.6 + 16320.6 + 15813.6 + 16835.6 + 19252.1 + 14823.5 + 16577.1 + 15068.1 + 13631 + 18158.1 + 17889.6 + 16835.6 + 15314.6 + 15314.6 + 16320.6 + 17358.6 + 15563.1 + 18158.1 + 18158.1 + 16835.6 + 17889.6 + 16577.1 + 2.53294e+08 + 17889.6 + 19252.1 + 17623.1 + 18975.6 + 12266 + 19530.6 + 18158.1 + 18158.1 + 17096.1 + 17623.1 + 19530.6 + 19252.1 + 21536.1 + 18428.6 + 19811.1 + 17096.1 + 19811.1 + 18428.6 + 18975.6 + 18158.1 + 18975.6 + 17096.1 + 19530.6 + 18158.1 + 16577.1 + 18701.1 + 16577.1 + 19811.1 + 16066.1 + 18158.1 + 14340.5 + 17623.1 + 17358.6 + 18701.1 + 16577.1 + 16320.6 + 18975.6 + 19252.1 + 16320.6 + 13398.5 + 18975.6 + 15563.1 + 17889.6 + 16835.6 + 14102 + 11183.5 + 18158.1 + 18158.1 + 17623.1 + 17623.1 + 14581 + 16835.6 + 16066.1 + 17889.6 + 14340.5 + 17889.6 + 18158.1 + 17889.6 + 16066.1 + 18701.1 + 20664.6 + 20093.6 + 18158.1 + 21830.7 + 17623.1 + 20093.6 + 17358.6 + 19811.1 + 19530.6 + 19530.6 + 18975.6 + 18975.6 + 19530.6 + 19252.1 + 20953.1 + 20378.1 + 17889.6 + 17889.6 + 17358.6 + 19252.1 + 18975.6 + 18158.1 + 16835.6 + 18158.1 + 18975.6 + 14340.5 + 16835.6 + 15813.6 + 19811.1 + 17096.1 + 17889.6 + 14102 + 15813.6 + 19252.1 + 18158.1 + 16577.1 + 20093.6 + 20378.1; printf "%f\n", a}'

255411071.499999

# 11  
Old 06-13-2012
Quote:
Originally Posted by SkySmart
the perl solution seems to work. does anyone know how to use awk for this?
awk...

Code:
echo '15314.6 16320.6 15813.6 16835.6 19252.1 14823.5 16577.1 15068.1 13631 18158.1 17889.6 16835.6 15314.6 15314.6 16320.6 17358.6 15563.1 18158.1 18158.1 16835.6 17889.6 16577.1 253294000 17889.6 19252.1 17623.1 18975.6 12266 19530.6 18158.1 18158.1 17096.1 17623.1 19530.6 19252.1 21536.1 18428.6 19811.1 17096.1 19811.1 18428.6 18975.6 18158.1 18975.6 17096.1 19530.6 18158.1 16577.1 18701.1 16577.1 19811.1 16066.1 18158.1 14340.5 17623.1 17358.6 18701.1 16577.1 16320.6 18975.6 19252.1 16320.6 13398.5 18975.6 15563.1 17889.6 16835.6 14102 11183.5 18158.1 18158.1 17623.1 17623.1 14581 16835.6 16066.1 17889.6 14340.5 17889.6 18158.1 17889.6 16066.1 18701.1 20664.6 20093.6 18158.1 21830.7 17623.1 20093.6 17358.6 19811.1 19530.6 19530.6 18975.6 18975.6 19530.6 19252.1 20953.1 20378.1 17889.6 17889.6 17358.6 19252.1 18975.6 18158.1 16835.6 18158.1 18975.6 14340.5 16835.6 15813.6 19811.1 17096.1 17889.6 14102 15813.6 19252.1 18158.1 16577.1 20093.6 20378.1' | tr ' ' '\n' | awk '{s+=$0}END{print s}'

This User Gave Thanks to Leion For This Post:
# 12  
Old 06-13-2012
Quote:
Originally Posted by ddreggors
Example:

Code:
$ echo ""|awk '{a=15314.6 + 16320.6 + 15813.6 + 16835.6 + 19252.1 + 14823.5 + 16577.1 + 15068.1 + 13631 + 18158.1 + 17889.6 + 16835.6 + 15314.6 + 15314.6 + 16320.6 + 17358.6 + 15563.1 + 18158.1 + 18158.1 + 16835.6 + 17889.6 + 16577.1 + 2.53294e+08 + 17889.6 + 19252.1 + 17623.1 + 18975.6 + 12266 + 19530.6 + 18158.1 + 18158.1 + 17096.1 + 17623.1 + 19530.6 + 19252.1 + 21536.1 + 18428.6 + 19811.1 + 17096.1 + 19811.1 + 18428.6 + 18975.6 + 18158.1 + 18975.6 + 17096.1 + 19530.6 + 18158.1 + 16577.1 + 18701.1 + 16577.1 + 19811.1 + 16066.1 + 18158.1 + 14340.5 + 17623.1 + 17358.6 + 18701.1 + 16577.1 + 16320.6 + 18975.6 + 19252.1 + 16320.6 + 13398.5 + 18975.6 + 15563.1 + 17889.6 + 16835.6 + 14102 + 11183.5 + 18158.1 + 18158.1 + 17623.1 + 17623.1 + 14581 + 16835.6 + 16066.1 + 17889.6 + 14340.5 + 17889.6 + 18158.1 + 17889.6 + 16066.1 + 18701.1 + 20664.6 + 20093.6 + 18158.1 + 21830.7 + 17623.1 + 20093.6 + 17358.6 + 19811.1 + 19530.6 + 19530.6 + 18975.6 + 18975.6 + 19530.6 + 19252.1 + 20953.1 + 20378.1 + 17889.6 + 17889.6 + 17358.6 + 19252.1 + 18975.6 + 18158.1 + 16835.6 + 18158.1 + 18975.6 + 14340.5 + 16835.6 + 15813.6 + 19811.1 + 17096.1 + 17889.6 + 14102 + 15813.6 + 19252.1 + 18158.1 + 16577.1 + 20093.6 + 20378.1; printf "%f\n", a}'

255411071.499999

i beleive this is what i need. it gives the precise numbers. however, if put all those numbers in a variable, it doesn't work.
Code:
echo ""|awk "{a='${JUMBO}'; printf "%f\n", a}"

any ideas?
# 13  
Old 06-13-2012
Code:
echo ""|awk "{a=${JUMBO}; printf \"%f\n\", a}"

---------- Post updated at 03:14 AM ---------- Previous update was at 01:14 AM ----------

or if you prefer this way:

Code:
$ echo "$JUMBO" |awk 'BEGIN{FS="\n";RS=" ";}{s+=$0}END{printf "%f\n", s}'
255411071.499999

This User Gave Thanks to ddreggors For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding a timestamp every N row in another column

Hi, i have a raw output file like this 167,63.50 167,63.50 168,63.68 166,63.68 168,63.68 I would like to add every each N rows (for example 60) and in a third column , a timestamp using the command date +"%H:%M"how can i do it with one single command ? Thank you !! (5 Replies)
Discussion started by: Board27
5 Replies

2. Shell Programming and Scripting

Sum of numbers in row

Need help in coding: File with several rows incl. numbers like 1 2 3 4 5 6 7 8 ... How can i build the sum of each row seperately? 10 26 ... Thx for help. Please use CODE tags as required by forum rules! (13 Replies)
Discussion started by: smitty11
13 Replies

3. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

4. Shell Programming and Scripting

Multiply a row of numbers

the following is used to add numbers: echo 7 47 47 44 4 3 3 3 3 3 | awk '{ for(i=1; i<=NF;i++) j+=$i; print j; j=0 }' how do i multiply OR subtract a row of numbers using the above tactic? (8 Replies)
Discussion started by: SkySmart
8 Replies

5. UNIX for Dummies Questions & Answers

Print last row of numbers

I have a spreadsheet of extremely long rows of numbers. I want to print only the last column. Tried using printf but there seems to be too many rows. example: 3 100 34 7 23 0 8 ..... X 400 203 778 1 ..........Y 58 3 9 0 100 ..........Z I only want to print X, Y and... (1 Reply)
Discussion started by: jimmyf
1 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Add row of numbers

Hi, Trying to add a row of numbers. There are 24 number across. Would like to have column 25 sum each row. 10 3 45 49 0 24... Sum 3 200 3 9 1 3 ...... Sum 9 7 20 9 8 10 ...... Sum Thank you. (5 Replies)
Discussion started by: jimmyf
5 Replies

7. UNIX for Dummies Questions & Answers

Adding a column to a text file with row numbers

Hi, I would like to add a new column containing the row numbers to a text file. How do I go about doing that? Thanks! Example input: A X B Y C D Output: A X 1 B Y 2 C D 3 (5 Replies)
Discussion started by: evelibertine
5 Replies

8. Shell Programming and Scripting

extracting non-zero pairs of numbers from each row

Hi all, I do have a tab delimited file a1 a2 b1 b2 c1 c2 d1 d2 e1 e2 f1 f2 0 0 123 546 0 0 0 0 0 0 0 0 0 0 345 456 765 890 902 1003 0 0 0 0 534 768 0 0 0 0 0 0 0 0 0 0 0 0 0 0 456 765 0 0 0 0 0 0 0 0 0 0 0 0 12 102 0 0 0 0 456 578 789 1003 678 765 345 400 801 1003 134 765... (5 Replies)
Discussion started by: Lucky Ali
5 Replies

9. Shell Programming and Scripting

counting the numbers in a row

File A aa <space> --D--A--D---DDY---M--UM-M--MY Another file D3 M9 So output shud be Here in FileA D which is 3 after removing dash after we have counted dash D is position at 9 and for M is 23 final output will be D9 M23 (2 Replies)
Discussion started by: cdfd123
2 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question