Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Add the values of the lines according to a condition

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
aynar's Unix or Linux Image
aynar aynar is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 20 April 2018, 11:59 AM EDT
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Add the values of the lines according to a condition

hi everybody Linux
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 09:51 AM..
Sponsored Links
    #2  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
rbatte1's Unix or Linux Image
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 24 May 2018, 12:44 PM EDT
Location: Lancashire, UK
Posts: 3,535
Thanks: 1,558
Thanked 692 Times in 622 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
aynar's Unix or Linux Image
aynar aynar is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 20 April 2018, 11:59 AM EDT
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
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 10:56 AM.. Reason: code tags, please!
    #4  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
wbport's Unix or Linux Image
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 25 May 2018, 5:01 PM EDT
Location: Mississippi
Posts: 139
Thanks: 36
Thanked 21 Times in 21 Posts
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 10:53 AM.. Reason: P.S.
Sponsored Links
    #5  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 27 May 2018, 10:24 PM EDT
Location: Foxborough, MA
Posts: 8,283
Thanks: 246
Thanked 824 Times in 769 Posts
do you want to group or to sum up by ip?
The Following User Says Thank You to vgersh99 For This Useful Post:
aynar (04-20-2018)
Sponsored Links
    #6  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
aynar's Unix or Linux Image
aynar aynar is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 20 April 2018, 11:59 AM EDT
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #7  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 27 May 2018, 10:24 PM EDT
Location: Foxborough, MA
Posts: 8,283
Thanks: 246
Thanked 824 Times in 769 Posts


Code:
awk -F'[ :]' '{a[$(NF-1)]+=$1} END{for(i in a) printf("%s -> %.7f\n", i, a[i])}' myFile

The Following User Says Thank You to vgersh99 For This Useful Post:
aynar (04-20-2018)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove duplicate values with condition jiam912 Shell Programming and Scripting 2 06-23-2014 06:22 PM
Grep certain lines with condition radius UNIX for Dummies Questions & Answers 4 05-07-2013 04:04 PM
Perl XML, find matching condition and grep lines and put the lines somewhere else tententen Shell Programming and Scripting 4 11-03-2010 08:55 PM
To read the values and to use in the where condition venkatesht Shell Programming and Scripting 10 08-12-2010 04:01 AM
Help!! Need script to read files and add values by lines... dhuertas Shell Programming and Scripting 3 09-15-2007 11:14 PM



All times are GMT -4. The time now is 11:19 PM.