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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
Add column and multiply its result to all elements of another column

INPUT FIILE
Code:
1    |   6
2   |   7
3   |  8
4  |   9
5   | 10

OUTPUT REQUIRED (sum of the first column $1*$2)

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

Moderator's Comments:
Mod Comment Please enclose sample input, sample output, and code segments in CODE tags as required by forum rules.

Last edited by Don Cragun; 1 Week Ago at 02:48 PM..
# 2  
Old 1 Week Ago
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.)
# 3  
Old 1 Week Ago
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
Code:
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 :

Code:
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 ---

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

Geting this output

Code:
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
Code:
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

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 1 Week Ago at 03:51 PM.. Reason: Added CODE tags.
# 4  
Old 1 Week Ago
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"?
This User Gave Thanks to RudiC For This Post:
Sagar Singh (1 Week Ago)
# 5  
Old 1 Week Ago
Dear sir, please check now.

using this command "
Code:
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
Code:
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
Code:
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


Last edited by Neo; 1 Week Ago at 12:49 AM..
Old 1 Week Ago
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:
Code:
$ 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.

Last edited by RudiC; 1 Week Ago at 05:37 PM..
This User Gave Thanks to RudiC For This Post:
Sagar Singh (1 Week Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Add Column base on other Column Data pareshkp Shell Programming and Scripting 5 10-28-2015 03:55 PM
Matching column and search closest elements giuliangiuseppe Shell Programming and Scripting 2 07-09-2014 07:18 AM
Count common elements in a column owwow14 Shell Programming and Scripting 4 12-13-2013 08:08 AM
Add all 4 column entries for similar column ids Diya123 Shell Programming and Scripting 7 12-11-2013 11:57 PM
Counting specific column and add result in output owwow14 Shell Programming and Scripting 4 10-31-2013 01:09 PM
Add a character C in a column if that column is blank thejitha Shell Programming and Scripting 21 08-28-2013 01:02 PM
Multiply certain column to variable guns Shell Programming and Scripting 6 03-07-2012 03:34 PM
Filtering lines for column elements based on corresponding counts in another column polsum Shell Programming and Scripting 1 03-05-2012 09:28 AM
to add special tag to a column based on column condition ckwan Shell Programming and Scripting 9 11-30-2011 12:54 AM
Average for repeated elements in a column FelipeAd UNIX for Dummies Questions & Answers 1 02-15-2011 11:44 AM
Need to add letters to a column and add in a new column subtracting from another column kellywilliams Shell Programming and Scripting 3 01-27-2011 09:50 PM
Multiply whole column with a value yale_work Shell Programming and Scripting 5 12-28-2010 08:49 PM
Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 rydz00 Shell Programming and Scripting 7 11-09-2010 10:28 AM
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM
Parse 1 column and add 2nd column sdlennon Shell Programming and Scripting 2 05-12-2009 02:51 PM