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


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Add column and multiply its result to all elements of another column
# 1  
Old 03-10-2019
Add column and multiply its result to all elements of another column

Input file is as follows:
Code:
1    |   6
2   |   7
3   |  8
4  |   9
5   | 10

Output reuired (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 RavinderSingh13; 04-09-2019 at 10:45 AM.. Reason: Removed unnecessary caps from user's question.
# 2  
Old 03-10-2019
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 03-10-2019
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; 03-10-2019 at 04:51 PM.. Reason: Added CODE tags.
# 4  
Old 03-10-2019
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:
# 5  
Old 03-10-2019
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; 03-11-2019 at 01:49 AM..
Old 03-10-2019
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; 03-10-2019 at 06:37 PM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiply elements of 2 arrays together into another array

So I need to Write an array processing program using a Linux shell programming language to perform the following. Load array X of 20 numbers from an input file X. Load array Y of 20 numbers from an input file Y. Compute array Z by multiply Xi * Yi then compute the square-root of this... (2 Replies)
Discussion started by: sarapham409
2 Replies

2. Shell Programming and Scripting

Matching column and search closest elements

Hi all I have a great challenge that I am not able to resolve. Briefly, I have a file like this: ID_1 chr1 100 - ID_2 chr2 300 + and another file like this: name_1 chr1 150 no - name_2 chr1 250 yes - name_3 chr2 350 yes + name_4 chr2 280 yes + Well, for each entry in file1 I would... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

3. Shell Programming and Scripting

Count common elements in a column

HI, I have a 3-column tab separated column (approx 1GB) in which I would like to count and output the frequency of all of the common elements in the 1st column. For instance: If my input was the following: dot is-big 2 dot is-round 3 dot is-gray 4 cat is-big 3 hot in-summer 5 My... (4 Replies)
Discussion started by: owwow14
4 Replies

4. Shell Programming and Scripting

Counting specific column and add result in output

Hi all, I have a quick question: I have a 4 column tab-separated file. I want to count the number of times each unique value in column 2 appears and add that number in a 5th column. I have the following input file: waterline-n below-sheath-v 14.8097 A dock-n below-sheath-v ... (4 Replies)
Discussion started by: owwow14
4 Replies

5. Shell Programming and Scripting

Multiply certain column to variable

Hi experts, I want to multiply certain columns to variable, data : 1 2 3 4 5 6 7 8 9 result with var = 2 for column 3,6,9 ... (every columns which can be divided to 3): 1 2 6 4 5 12 7 8 18 I have tried : awk 'BEGIN{FS=OFS=" "}{print $1,$2,$3*a,$4,$5,$6*a,$7,$8,$9*2 }' a=2 file.txt but how... (6 Replies)
Discussion started by: guns
6 Replies

6. Shell Programming and Scripting

Filtering lines for column elements based on corresponding counts in another column

Hi, I have a file like this ACC 2 2 21 aaa AC 443 3 22 aaa GCT 76 1 33 xxx TCG 34 2 33 aaa ACGT 33 1 22 ggg TTC 99 3 44 wee CCA 33 2 33 ggg AAC 1 3 55 ddd TTG 10 1 22 ddd TTGC 98 3 22 ddd GCT 23 1 21 sds GTC 23 4 32 sds ACGT 32 2 33 vvv CGT 11 2 33 eee CCC 87 2 44... (1 Reply)
Discussion started by: polsum
1 Replies

7. Shell Programming and Scripting

to add special tag to a column based on column condition

Hi All, I have following html code <TR><TD>9</TD><TD>AR_TVR_TBS </TD><TD>85000</TD><TD>39938</TD><TD>54212</TD><TD>46</TD></TR> <TR><TD>10</TD><TD>ASCV_SMY_TBS </TD><TD>69880</TD><TD>33316</TD><TD>45698</TD><TD>47</TD></TR> <TR><TD>11</TD><TD>ARC_TBS ... (9 Replies)
Discussion started by: ckwan
9 Replies

8. UNIX for Dummies Questions & Answers

Average for repeated elements in a column

I have a file that looks like this 452 025_E3 8 025_E3 82 025_F5 135 025_F5 5 025_F5 23 025_G2 38 025_G2 71 025_G2 9 026_A12 81 026_A12 10 026_A12 some of the elements in column2 are repeated. I want an output file that will extract the... (1 Reply)
Discussion started by: FelipeAd
1 Replies

9. Shell Programming and Scripting

Need to add letters to a column and add in a new column subtracting from another column

So I have this input 1 10327 rs112750067 T C . PASS DP=65;AF=0.208;CB=BC,NCBI 1 10469 rs117577454 C G . PASS DP=2055;AF=0.020;CB=UM,BC,NCBI 1 10492 rs55998931 C T . PASS DP=231;AF=0.167;CB=BC,NCBI 1 10583 rs58108140 G A ... (3 Replies)
Discussion started by: kellywilliams
3 Replies

10. Shell Programming and Scripting

Multiply whole column with a value

Hi, I need to multiply 3rd column (comma seperated) of entire file by a value say 2.2. Suppose the file is: C,Gas $ YTD(TRI),15512.36,01/01/2010 New file should be (3rd column value multiplied by 2.2): C,Gas $ YTD(TRI),34127.192,01/01/2010 (5 Replies)
Discussion started by: yale_work
5 Replies
Login or Register to Ask a Question