Mathematical Operations on Column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mathematical Operations on Column
# 1  
Old 09-20-2015
Mathematical Operations on Column

Hi All,
I want to perform a mathematical operation on column. Can anyone please help?

Here is the sample of operation to be performed:
Code:
123 996 100 
123 996 200 
123 996 200 2015-09-21
123 996 100 
123 996 200 
123 996 100

What I want is to multiple all values of column # 3 by 100 and then summing up all those.

Best Regards,,,

Last edited by Don Cragun; 09-20-2015 at 06:28 PM.. Reason: Add CODE tags.
# 2  
Old 09-20-2015
What have you tried?

What operating system and shell are you using?

Last edited by Don Cragun; 09-20-2015 at 06:33 PM.. Reason: Ask about OS and shell.
# 3  
Old 09-20-2015
Hey Don,
I haven't ever used this operation before but now want to learn and use it.

OS is Solaris.
# 4  
Old 09-20-2015
I'm surprised that you have never used addition before...

Assuming that your data is in a file named file, you could try something like:
Code:
#!/bin/ksh
sum=0
while read x x v3 x
do	sum=$((sum + v3))
done < file
printf '%d00\n' "$sum"

Note that multiplying by 100 can be done by performing an actual multiplication or by just adding two zeros when you print the results. Adding zeros while printing the results lets you handle larger numbers without overflowing the values that the shell can hold in an object of type signed long integer.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mathematical calculations using shell

Dear All, I read some variables in a file and assigned as name for each of them. If I do echo I am able to see the values as 1.0E-05,3.4,5.0E-03 etc, Now I want to do some mathematical operations with them. Lets say 1 1.0E-05*5.0E-03 expected ans is 5.0E-08 2 1.0E-05/5.0E-03 expected... (9 Replies)
Discussion started by: linuxUser_
9 Replies

2. Homework & Coursework Questions

Help mathematical shell programming

Hello Guys,For my homework I must write a shell script to do this serie, http://upload.wikimedia.org/math/f/8/f/f8f543d9ecd01c4ecca2a0b7bc1234a2.pngI know that I must use the "bc" for that, but for the script's itself i have no idea,(beginner) Can you plz just help me for have some idea?... (1 Reply)
Discussion started by: hamed.samie
1 Replies

3. Homework & Coursework Questions

Mathematical scripting question

Hello Guys,For my homework I must write a shell script to do this serie, http://upload.wikimedia.org/math/f/8/f/f8f543d9ecd01c4ecca2a0b7bc1234a2.pngI know that I must use the "bc" for that, but for the script's itself i have no idea,(beginner) Can you plz just help me for have some idea?... (1 Reply)
Discussion started by: hamed.samie
1 Replies

4. UNIX for Dummies Questions & Answers

Adding a column to a text file based on mathematical manipulation

Hi, I have a tab delimited text file with three different columns. I want to add an extra column to the text file. The extra column will be the second column and it will equal third column - 1. How do I go about doing that? Thanks! Input: chr1 788822 rs11240777 chr1 1008567 rs9442372... (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

Mathematical calculations in shellscript

Hi want to do below mathematical calculations in shellscrip but it is not giving me the exact output.Please help me to solve this price=95.3 price1=(20/100)*$price+$price echo "price=$price1" finally the output should display price=114.36 Thanks (5 Replies)
Discussion started by: aish11
5 Replies

6. Shell Programming and Scripting

Mathematical Loop

Hi, I'm creating a loop that allows the user to enter any number, then their choice of operator and then another number until their operator choice is equal to = But I am getting an error saying integer expression expected. Any explanation on why this is happening? echo "Please enter a number"... (1 Reply)
Discussion started by: Addman1991
1 Replies

7. Shell Programming and Scripting

Can BASH handle mathematical operations and do a Search & Replace?

Hello, I have a bunch of xml file that needs to have edits made and I was wondering if a BASH script could handle it. I would like the script to look within my xml files and replace all integers greater than 5px with a value that is 25% smaller. For example, 100px = 75px. Since the integers... (12 Replies)
Discussion started by: jl487
12 Replies

8. UNIX for Dummies Questions & Answers

arithmetic operations on 1 column of a file

Hi, I have a file with thousands of lines like this: Chr1 477515 . ACCCC ACCC 17.7 . INDEL;DP=17;AF1=1;CI95=0.5,1;DP4=0,1,0,3;MQ=32;PV4=1,0.036,1,1 Chr1 481987 . A AAAT 62 . INDEL;DP=11;AF1=1;CI95=0.5,1;DP4=0,0,1,3;MQ=41 I want to make a file with... (2 Replies)
Discussion started by: fadista
2 Replies

9. Shell Programming and Scripting

awk command: column operations

I have a txt file with two columns Freq Loss 10 30 20 40 30 10 40 50 50 60i have used the below code to get the minimum value out of this array in second cloumn. awk 'NR==N{min=$N;max=$N}NR>N{if ($N>max){max=$N};if ($N<min){min=$N}}END {print... (3 Replies)
Discussion started by: shashi792
3 Replies

10. Shell Programming and Scripting

Error in mathematical calculation using bc

I am getting the error: Runtime error (func=(main), adr=10): Divide by zero When executing the mathematical expression: echo "scale=2; 1-(0/0)"|bc How to overcome this? (5 Replies)
Discussion started by: proactiveaditya
5 Replies
Login or Register to Ask a Question