Adding row of numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding row of numbers
# 1  
Old 06-12-2012
Adding row of numbers

is there another way of doing the below:
Code:
echo "7 3 8 2 2 1 3 83.4 8.2 4 8 73 90.5" | bc

shell is bash. os is linux and sunos.

bc seems to have an issue with long range of numbers
# 2  
Old 06-12-2012
I (and bc) do not understand what you want to do here.

Are you adding?
Code:
$ echo "7 + 3 + 8 + 2 + 2 + 1 + 3 + 83.4 + 8.2 + 4 + 8 + 73 + 90.5" | bc
293.1

you have not given us or bc enough information in that command.
# 3  
Old 06-12-2012
bc appears to be unreliable:
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 + 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" | bc
(standard_in) 1: syntax error

# 4  
Old 06-12-2012
bc does not support exponential notation.

Regards,
Alister
# 5  
Old 06-12-2012
nope, just doesn't understand what your giving it:

Code:
$ echo "2.53294e+08 + 5" |bc
(standard_in) 1: syntax error

The "e+" (exponential) notation is a bit to complicated for bc. Convert these first to decimal like "253294000" and try again:

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" | bc

255411071.5

# 6  
Old 06-12-2012
i dont want to have to convert anything. can another program handle this? possibly awk
# 7  
Old 06-12-2012
AWK does support exponential notation.
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