Multiply a row of numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiply a row of numbers
# 1  
Old 01-20-2014
Multiply a row of numbers

the following is used to add numbers:
Code:
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?
# 2  
Old 01-20-2014
Try:

Code:
echo 7 47 47 44 4 3 3 3 3 3 | awk '{ for(j=i=1; i<=NF;i++) j*=$i; print j; j=0 }'

or
Code:
echo 7 47 47 44 4 3 3 3 3 3 | awk '{ j=1; for(i=1; i<=NF;i++) j*=$i; print j; j=0 }'

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-21-2014
Quote:
Originally Posted by Chubler_XL
Try:

Code:
echo 7 47 47 44 4 3 3 3 3 3 | awk '{ for(j=i=1; i<=NF;i++) j*=$i; print j; j=0 }'

or
Code:
echo 7 47 47 44 4 3 3 3 3 3 | awk '{ j=1; for(i=1; i<=NF;i++) j*=$i; print j; j=0 }'

both worked perfectly.

i tried modifying it on my own to also substract numbers and i cant seem to get that work:
Code:
echo 1700 36 3 2 788 22  | awk '{ j=1; for(i=1; i<=NF;i++) j-=$i; print j; j=0 }'
-2550

also tried this:

Code:
echo 1700 36 3 2 788 22  | awk '{ j=1; for(i=1; i<=NF;i--) j-=$i; print j; j=0 }'

# 4  
Old 01-21-2014
-2550 is the exact solution, so what be your problem?
# 5  
Old 01-21-2014
Code:
 1700 - 36 - 3 - 2 - 788 - 22 = 849

the correct amount should be 849
# 6  
Old 01-21-2014
Quote:
Originally Posted by SkySmart
Code:
 1700 - 36 - 3 - 2 - 788 - 22 = 849

the correct amount should be 849
Here is 849

Code:
$ echo 1700 36 3 2 788 22  | awk '{ j=$1; for(i=2; i<=NF;i++) j-=$i; print j; j=0}'
849

Code:
$ echo 1700 36 3 2 788 22  | awk '{ for(i=2; i<=NF;i++) j+=$i; print $1-j;j=0}'
849

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 01-21-2014
Quote:
Originally Posted by SkySmart
Code:
 1700 - 36 - 3 - 2 - 788 - 22 = 849

the correct amount should be 849
This is not what you programmed. Your equation is 1 - 1700 - 36 - 3 - 2 - 788 - 22 = -2550
You need to take special action - as Akshay Hegde did - to use one of the elements as minuend.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

Multiply value by row

Hi I would like to know how can I multiply the value of column three to columns 4-end of file example of input file: rs1000012 AK8 2 0.05 0.05 1 0 0 rs10000154 PAQR3 0.01 2 1 2 2 1 Desired output file: rs1000012 AK8 ... (1 Reply)
Discussion started by: fadista
1 Replies

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

Adding row of numbers

is there another way of doing the below: 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 (12 Replies)
Discussion started by: SkySmart
12 Replies

7. Shell Programming and Scripting

Multiply numbers in two columns and then add.

I have some 100 files with extension .tmp The files are named as 1.tmp, 2.tmp, 3.tmp until 100.tmp All files look like this: 0.38701788 1.968068e-02 0.38622013 2.054002e-02 0.38350296 1.715522e-02 0.38282126 1.781283e-02 0.38282126 1.781283e-02 0.35847232 1.026839e-02 0.3557739... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

8. Shell Programming and Scripting

Multiply numbers from different files

Hi All, I have tried few things with this but it did not help much. I have some 200,000 files in a directory. There are two sets of files. 1. Files with extension .dat with file names like these (1.dat, 2.dat, 5.dat, 8.dat....200000.dat) 2. Another set of files with .txt extension and... (5 Replies)
Discussion started by: shoaibjameel123
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. UNIX for Dummies Questions & Answers

How to add/multiply numbers with scientific notation (2.343e-5)

Hi, I'm need to do some addition and multiplication of scientific nottaion numbers, in the form 34.23423e-10 for example. I was echoing the list of numbers to stdout, then using bc -l, then I find that this does not seem to work for numbers with exponential notation. Could someone help me out... (1 Reply)
Discussion started by: chugger06
1 Replies
Login or Register to Ask a Question