Help needed with multiplying two values of two columns in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed with multiplying two values of two columns in a file
# 1  
Old 07-26-2012
Help needed with multiplying two values of two columns in a file

Hi, I am trying to multiply column#1 with column#2 using a shell script. How can I make a for-loop script using 1st column as "i" and the second column as "j" from the following file? Please feel free to share any alternative ways to multiplying column#1 with column#2.

HTML Code:
.06     5.0000
.49     7.8750
.18     5.0000
.17     1.0000
.64     6.1250
.01     0.1250
.48     4.81250
.01     0.2500
.77     19.5000
Thanks in advance
Smilie

---------- Post updated at 08:20 PM ---------- Previous update was at 08:16 PM ----------

This is what I tried..... but did not work Smilie

HTML Code:
root@vna1(/)# for x in `awk '{print $1}' /tmp/temp6`
> do
> for j in `awk '{print $2}' /tmp/temp6`
> do
> echo "scale=3; $x * $y" | bc
> done
> done
syntax error on line 1 stdin
syntax error on line 1 stdin
syntax error on line 1 stdin
syntax error on line 1 stdin
syntax error on line 1 stdin
syntax error on line 1 stdin
syntax error on line 1 stdin
# 2  
Old 07-26-2012
so, you already try in awk, why not write all by awk?

Code:
awk '{printf "%.3f\n", $1*$2}' infile

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 07-27-2012
Hi rdcwayx,

You rock, it works!! Smilie

Thanks a bunch,
Momin

---------- Post updated at 02:28 PM ---------- Previous update was at 11:53 AM ----------

The output column in the solution above can have any number of values.... how can all those values be added when I we do not know the number of fields in the output?
# 4  
Old 07-27-2012
added or multiplied? you mean you can have any number of columns rather than just two?

Code:
awk '{p=$1;for(i=2;i<=NF;i++)p*=$i;printf("%.3f\n",p)}'

example:
Code:
$ echo 3 .5 2 | awk '{p=$1;for(i=2;i<=NF;i++)p*=$i;printf("%.3f\n",p)}'
3.000

This User Gave Thanks to neutronscott For This Post:
# 5  
Old 07-27-2012
Thanks neutronscott.

After adding up the two columns with rdcwayx's command, I had the output in form of one column. That column can have 15,16 or any number of values. My question was how to get the sum of all those values.

For example:
HTML Code:
Output:
12
5
8
10
I want to get the sum of the values: 12+5+8+10=35. But I do not know how many values will be in the output, because output is different on each server.
# 6  
Old 07-27-2012
assuming your input as
Code:
12
5
8
10


you can try this


Code:
awk '{while(i<NR){tot=tot+$1;i++}}END{print tot}' file

# 7  
Old 07-27-2012
oh i see. i think chidori's sum would be for columns. you can sum it in same go as your multiplication. building on rdcwayx's code:

Code:
$ awk '{p=$1*$2;sum+=p;printf("%.3f\n", p)}END{printf("total: %.3f\n",sum)}' infile
0.300
3.859
0.900
0.170
3.920
0.001
2.310
0.003
15.015
total: 26.477

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Add values to file in 2 new columns

Columns 4 and 5 are X and Y coordinates, column 6 is the elevation I would like to add 2 new columns at the end of the file with values the distance between first(X)(Y) and last location (X)(Y), based in 2 rows the difference in elevation = ($6-prev6) How to calculate the requested values... (6 Replies)
Discussion started by: jiam912
6 Replies

2. Shell Programming and Scripting

Creating a loop for multiplying columns

I have 2 files, that look like this: ID SNP1 SNP2 SNP3 SNP4 A1 1 2 0 2 A2 2 0 1 1 A3 0 2 NA 1 A4 1 1 0 2 and this: SNP score SNP1 0.5 SNP2 0.7 SNP3 0.8 SNP4 0.2 Basically, all of the SNP-values are 0,1, 2 or NA, and they each have a score, listed in the second file. The total... (5 Replies)
Discussion started by: kayakj
5 Replies

3. Shell Programming and Scripting

Needed value after the last delimeter in a file with varying number of delimited columns

Hi All, My file has the records as below: aaa\bbb\c\dd\ee\ff\gg zz\vv\ww pp\oo\ii\uu How can I get the value after the last delimeter. My o/p: gg ww uu Thanks in Advance, (5 Replies)
Discussion started by: HemaV
5 Replies

4. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. UNIX for Dummies Questions & Answers

Subtracting values from 2 columns in a file

Hello, I have a file with 5 columns that looks like this: A1BG chr19 + 58863335 58866549 A1BG chr19 - 58858171 58864865 A2LD1 chr13 - 101182417 101186056 A2LD1 chr13 - 101182417 101241046 A2M chr12 - 9220303 9268558 A2ML1 ... (5 Replies)
Discussion started by: wolf_blue
5 Replies

6. Shell Programming and Scripting

error- multiplying negative decimal values

a=10.02 pattern=-11.01 b=$(echo | awk '{ print $a*$pattern}') echo $b its not working even ALso tried `expr $a \* $pattern` No LUCK (3 Replies)
Discussion started by: saluja.deepak
3 Replies

7. UNIX for Dummies Questions & Answers

combine the values from the first two columns within a file

Hello everybody, I have a text file containing 10,000 rows and 5000 columns. The values are separated by a tab. Ex. file_ex.ped 1 mike 0 0 2 1 A A G G C T A G 1 jack 0 0 2 2 T A G T C A A C 1 Mary 0 0 1 2 A T G C A T G C ... I would like a out put file 1 mike 0 0 2 1 AA GG CT AG 1... (7 Replies)
Discussion started by: Unilearn
7 Replies

8. Shell Programming and Scripting

multiplying values from two text files

Im very new to programming. But I would like to write a script which extracts and multiply values from 2 txt and output as a new file. Can someone please teach me how to write it? Thank you so much for example File A File B 1 34 1 2 2 13 2 2 3 8 3 3 File C output 1 68 2... (2 Replies)
Discussion started by: crunchichichi
2 Replies

9. Shell Programming and Scripting

Help needed: Adding columns in csv file in loop

Hi Everyone: My shell script creates multiple csv files (~30) in for loop. I want to compile (or merge) 3rd column from each (all) of these files to another file (in loop). Please help. Thanks. (3 Replies)
Discussion started by: smap007
3 Replies

10. UNIX for Dummies Questions & Answers

Help needed to sort multiple columns in one file

Hi, I would like to know given that I have 3 columns. Let say I have first 3 columns to do operation and these operation output is printed out each line by line using AWK and associative array.Currently in the output file, I do a sort by -r for the operation output. The problem comes to... (1 Reply)
Discussion started by: ahjiefreak
1 Replies
Login or Register to Ask a Question