Add column and multiply its result to all elements of another column

Input file is as follows:

1    |   6
2   |   7
3   |  8
4  |   9
5   | 10  

Output reuired (sum of the first column $1*$2)

1    |   6     |  90
2     | 7  |     105
3     | 8    |   120
4      |9      | 135
5     |10     | 150

Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If you did not post homework, please explain the what you are working on that lead to these requirements.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

If this is not a homework assignment, please also explain why the <space>s are present in supposedly numeric fields using the <vertical-bar> symbol ( | ) as a field separator and explain how you are supposed to determine how many <space> characters need to be included in each output in your desired output>. (I don't see any pattern to the number of <space>s before or after either of the <vertical-bar> symbols in your required output.)

sir, I am a trainee in a telecom company and this is the daily routine work I have to do this daily, that's why I am trying to automate it using Linux scripting

Actual question is

mss1:BSCNND2  12367     6.3  
mss2:BSCNND2  11418     6.3  
mss3:BSCNND2  10945     6.3 
mss4:BSCNND2  10898     6.3 
mss6:BSCNND2  13298     7.4  
bc01:BSCNAN2  66546     34.5
bc02:BSCNND2  58478     32.9 

Required output is :

BSCNND2    12367    6.3      Sum of $2*6.3/100
BSCNND2    11418    6.3      Sum of $2*6.3/100
BSCNND2    10945    6.3      Sum of $2*6.3/100
BSCNND2    10898    6.3      Sum of $2*6.3/100
BSCNND2    13298    7.4      Sum of $2*7.4/100
BSCNAN2    66546    34.5     Sum of $2*34.5/100
BSCNND2    58478    32.9     Sum of $2*32.9/100

These values are changing on the basis of daily logs generated (earlier post was just an example)

--- Post updated at 07:28 PM ---

egrep "BSCNND2|BSCNAN2" mss? bc0? > bscnnd2 && paste bscnnd2 RLCAP.txt | awk '{sum+=$2} {print $0," "sum*$3}'

Geting this output

mss1:BSCNND2  12367     6.3  77912.1
mss2:BSCNND2  11418     6.3  149846
mss3:BSCNND2  10945     6.3  218799
mss4:BSCNND2  10898     6.3  287456
mss6:BSCNND2  13298     7.4  436052
bc01:BSCNAN2  66546     34.5 4328784
bc02:BSCNND2  58478     32.9 6051955

but required output is

BSCNND2    12367    6.3    1158885
BSCNND2    11418    6.3    1158885
BSCNND2    10945    6.3    1158885
BSCNND2    10898    6.3    1158885
BSCNND2    13298    7.4    1361230
BSCNAN2    66546    34.5   6346275
BSCNND2    58478    32.9   6051955

Please create your specifications with DUE care, and become way more precise, detailed, and consistent. No moving targets, esp. in one single post. Stop making people guess.

Why are the two "required outputs" different?

What is mss? ? What bc0? ?
What is the contents of RLCAP.txt ?
How come the first four lines have a constant fourth field?
How do they match the formula given: Sum of $2*6.3/100 etc.? What "Sum"?

1 Like

Dear sir, please check now.

using this command "

awk '{sum+=$2} {print $0," "sum*$3}'

" not getting the required output
Here I want to multiply the sum of all values of column 2 with each line of column 3 & show result in column 4

Input

BSCNND2|    12367|    6.3|
BSCNND2|    11418|    6.3|
BSCNND2|    10945|    6.3|
BSCNND2|    10898|    6.3|
BSCNND2|    13298|    7.4|
BSCNAN2|    66546|    34.5|
BSCNND2|    58478|    32.9|

Required Output

BSCNND2|    12367|    6.3|    1158885
BSCNND2|    11418|    6.3|    1158885
BSCNND2|    10945|    6.3|    1158885
BSCNND2|    10898    6.3|    1158885
BSCNND2|    13298|    7.4|    1361230
BSCNAN2|    66546|    34.5|6346275
BSCNND2|    58478|    32.9    |6051955

Community Rules

Do you want me to repeat my questions?
Where do the pipes come from, all of a sudden?

EDIT: OK, staring at your posts for another 15 minutes, I think / guess I understood your request. How far would this get you:

$ awk '{SUM += $2; F3[NR] = $3; T[NR] = $0} END {for (n=1; n<=NR; n++) print T[n], F3[n]*SUM}' file
BSCNND2 12367 6.3 1158885
BSCNND2 11418 6.3 1158885
BSCNND2 10945 6.3 1158885
BSCNND2 10898 6.3 1158885
BSCNND2 13298 7.4 1361230
BSCNAN2 66546 34.5 6346275
BSCNND2 58478 32.9 6051955

I've used one of your earlier input samples. Please excuse that I didn't dare to try to reproduce the pipes and spaces in your desired output.

1 Like