Add the values of the lines according to a condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add the values of the lines according to a condition
# 1  
Old 04-20-2018
Add the values of the lines according to a condition

hi everybody Smilie
I am a beginner in bash and I want to convert the result I have here
I want it to be grouped by IP address
so iwanna get for each ip adsress the addition of all bandwidth where ip is 100.1.1.15
in other words for 100.1.1.15 it groups me all the values whose ip address is 100.1.1.15
is it possible?
Code:
4,03446 MB              209.85.202.147:443 --> 100.1.1.15:3823
3,78212 MB              172.217.21.206:443 --> 100.1.1.15:3844
1,42352 MB              216.58.210.10:443 --> 100.1.1.15:3672
0,181173 MB             216.58.210.142:80 --> 100.1.1.15:3890
0,0822248 MB            216.58.210.131:443 --> 100.1.1.15:3826
0,0522203 MB            54.71.250.161:443 --> 100.1.1.64:63209
0,0475864 MB            54.71.250.161:443 --> 100.1.1.64:63211
0,0471096 MB            54.71.250.161:443 --> 100.1.1.64:63210
0,0458832 MB            52.49.225.22:443 --> 100.1.1.75:2541
0,0346823 MB            216.58.210.131:443 --> 100.1.1.15:3825
0,0327778 MB            104.20.42.93:443 --> 100.1.1.15:3888
0,0325193 MB            66.102.1.189:443 --> 100.1.1.75:2414
0,032095 MB             216.58.210.142:80 --> 100.1.1.15:3893


Last edited by vbe; 04-20-2018 at 10:51 AM..
# 2  
Old 04-20-2018
Welcome aynar,

I have a few to questions pose in response first:-
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.
# 3  
Old 04-20-2018
until now i've changed the output to that

Code:
4,03446                 100.1.1.15
3,78212                 100.1.1.15
1,42352                 100.1.1.15
0,181173                100.1.1.15
0,0822248               100.1.1.15
0,0522203               100.1.1.64
0,0475864               100.1.1.64
0,0471096               100.1.1.64
0,0458832               100.1.1.75
0,0346823               100.1.1.15
0,0327778               100.1.1.15
0,0325193               100.1.1.75
0,032095                100.1.1.15
0,0258636               100.1.1.15
0,0189037               100.1.1.18
0,0183878               100.1.1.134
0,0168571               100.1.1.18
0,016758                100.1.1.15
0,0154533               100.1.1.15
0,0149384               100.1.1.15


but i have no idea how to do the rest
i have the idea but i don't know how to apply it
i dont even know if it's possible to do a loop that verify in the second column and and show me for each ip the total of the corresponding number

Last edited by vgersh99; 04-20-2018 at 11:56 AM.. Reason: code tags, please!
# 4  
Old 04-20-2018
If you just want them listed together, the sort command would do that. if you only want "100.1.1.15". grep is something to try.

That's where I stop for now. You will learn it and retain it much better if you look up those commands and experiment with how to use them.

P.S. Just saw that you removed a few things from each line. That isn't necessary.

Last edited by wbport; 04-20-2018 at 11:53 AM.. Reason: P.S.
# 5  
Old 04-20-2018
do you want to group or to sum up by ip?
This User Gave Thanks to vgersh99 For This Post:
# 6  
Old 04-20-2018
thanks for your help

it's no about 100.1.1.15 it's just an exemple i want to know for how much bandwidth is consumed by each ip address
do for each ip i want to have the total of numbers

---------- Post updated at 09:58 AM ---------- Previous update was at 09:57 AM ----------

that's it sum up by ip
# 7  
Old 04-20-2018
Code:
awk -F'[ :]' '{a[$(NF-1)]+=$1} END{for(i in a) printf("%s -> %.7f\n", i, a[i])}' myFile

This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicate values with condition

Hi Gents, Please can you help me to get the desired output . In the first column I have some duplicate records, The condition is that all need to reject the duplicate record keeping the last occurrence. But the condition is. If the last occurrence is equal to value 14 or 98 in column 3 and... (2 Replies)
Discussion started by: jiam912
2 Replies

2. Shell Programming and Scripting

Print certain lines based on condition

Hi All, I have following listing Filesystem GB blocks Free Used Iused Iused Mounted on /dev/hd2 4.00 0.31 93 63080 43 /usr Filesystem GB blocks Free Used Iused Iused Mounted on Filesystem GB blocks Free Used Iused Iused... (11 Replies)
Discussion started by: ckwan
11 Replies

3. UNIX for Dummies Questions & Answers

Grep certain lines with condition

file input aaaa,52C aaaa,50C bbbb,50C bbbb,58C aaaa,52C bbbb,50C aaaa,30C bbbb,58C cccc,60C i want to print uniq lines with its max value of column2 expected output aaaa,52C bbbb,58C cccc,60C tks (4 Replies)
Discussion started by: radius
4 Replies

4. Shell Programming and Scripting

Print every 5 lines with special condition

Hi Friends, I have an input file like this chr1 100 200 chr1 200 300 chr1 300 400 chr1 400 500 chr1 500 600 chr1 600 700 chr1 700 800 chr1 800 900 chr1 900 920 chr1 940 960 I would like to get the first line's second column and the fifth line's 3rd column as one single line. This... (13 Replies)
Discussion started by: jacobs.smith
13 Replies

5. Shell Programming and Scripting

remove duplicate lines with condition

hi to all Does anyone know if there's a way to remove duplicate lines which we consider the same only if they have the first and the second column the same? For example I have : us2333 bbb 5 us2333 bbb 3 us2333 bbb 2 and I want to get us2333 bbb 10 The thing is I cannot... (2 Replies)
Discussion started by: vlm
2 Replies

6. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

7. Shell Programming and Scripting

To read the values and to use in the where condition

Hi, I have the below values in a text file. 'xx.16397950', 'xx.16397957', 'xx.16397976', 'xx.16473259', I need to use in the where clause of the sql query in my unix shell script. How to read one by one value. It should be like this: select * from <<table name>> where xx in (... (10 Replies)
Discussion started by: venkatesht
10 Replies

8. Shell Programming and Scripting

Removing lines with condition

Hello guys, I need help with a script for removing lines that does not satisfy a condition. For example if a file has these lines: aaaa bbbb cccc aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll aaaa bbbb cccc jjjj kkkk lllll dddd eeee ffff dddd eeee ffff Then I want... (4 Replies)
Discussion started by: jaysean
4 Replies

9. Shell Programming and Scripting

numbering lines according to a condition

hello again guys, I tried to make a script but due to array's limitations I didn't succeed...so I'm asking you :) I need numbering the lines according to date (everyday I need to restart the counter) for example: ABCBD 20080101 XXX 1 FSDFD 20080101 BBB 2 FSDFD 20080102 HHH 1 and so... (3 Replies)
Discussion started by: elionba82
3 Replies

10. Shell Programming and Scripting

Help!! Need script to read files and add values by lines...

Hi All, I really need your help. I am a begginner in shell script and I believe this is a very simple issue. I have in my directory, n-files, like 1.dhm, 2.dhm, 3.dhm. These files have 1 column with 1 value per line, like: 1.dhm ------ 10 20 30 40 50 2.dhm ------ 30 50 20 (3 Replies)
Discussion started by: dhuertas
3 Replies
Login or Register to Ask a Question