The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to sort, and count unique data all at once? amatuer_lee_3 Shell Programming and Scripting 16 04-14-2009 10:22 PM
Source and Target count checking ksmbabu Shell Programming and Scripting 2 04-03-2008 03:57 AM
grep running total/ final total across multiple files MrAd UNIX for Dummies Questions & Answers 5 05-08-2007 01:03 PM
Byte Rate to/from a source/destination cubs0729 UNIX for Advanced & Expert Users 1 08-08-2005 04:55 PM
Script to count unique number of user loged in elchalateco Shell Programming and Scripting 1 09-30-2002 11:32 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-09-2008
new_buddy new_buddy is offline
Registered User
  
 

Join Date: May 2008
Posts: 11
Count total unique destination for source

Hi, need help how to count unique destination for the source ip. The file is contains 4 number of fields. Example of the file is here
HTML Code:
src ip           dest ip     #of flows   total bytes
192.168.6.0  88.0.33.2      12           128
192.168.6.0  88.0.33.2      1             168
192.168.6.0  111.22.35.0   2             364
192.168.5.0  88.0.33.2      1             125
.....
I want to count the unique destination, total flows and total bytes per sorce ip. The output that I want it to be is this
HTML Code:
192.168.6.0   2    15   660
192.168.5.0   1    1    125
.....
Is anyone can help me?
  #2 (permalink)  
Old 07-09-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,869
Your output seems worng, given your sample input.
Anyway, I suppose you need something like this:
(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk 'END {
  for (k in c) {
    split(k, t, SUBSEP)
    print t[1], c[k], f[k], b[k]
  }
}  
NR > 1 { 
  c[$1,$2] ++ 
  f[$1,$2] += $3 
  b[$1,$2] += $4 
  }' file
  #3 (permalink)  
Old 07-09-2008
new_buddy new_buddy is offline
Registered User
  
 

Join Date: May 2008
Posts: 11
I am not sure what is wrong with my output. It is the output that I want.

Quote:

src ip dest ip #of flows total bytes
192.168.6.0 88.0.33.2 12 128
192.168.6.0 88.0.33.2 1 168
192.168.6.0 111.22.35.0 2 364
192.168.5.0 88.0.33.2 1 125
.....I want to count the unique destination, total flows and total bytes per sorce ip. The output that I want it to be is this

192.168.6.0 2 15 660
192.168.5.0 1 1 125
.....
From here, source 192.168.6.0 has 2 unique destination (88.0.33.22 and 111.22.35.0). and total of the flows is 12 (12+1+2) and total bytes 660 (128+168+364). So. from your code, I am trying to fit with my code. I am using awk -f count.awk. This are the lines in the count.awk file.
Code:
            {src[$1,$2]++
	total[$1,$2]=total[$1,$2]+$3 
	bytes[$1,$2]=bytes[$1,$2]+$4 
	}
 	END{
	for (i in src) print src " " src[i] "\t" total[i] "\t"  bytes[i] 
	}
However, I got an error said that the src array is an illegal reference. Please help me. Need to solve this problem
  #4 (permalink)  
Old 07-10-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,869
Yep,
your requirement is more than clear,
sorry for the noise.
Use this code:

Code:
awk 'END {
  for (k in u) {
    printf "%s\t%s\t%s\t%s\n", 
	  k, u[k], f[k], b[k]
  }
}  
NR > 1 { 
  _[$1,$2]++ ? u[$1] : ++u[$1] 
  f[$1] += $3 
  b[$1] += $4 
  }' file
  #5 (permalink)  
Old 07-10-2008
new_buddy new_buddy is offline
Registered User
  
 

Join Date: May 2008
Posts: 11
Thumbs up

Many thanks for your help. I am now manage to produce the output I want. However, when I manually check, there is line of output is miscalculate. When I try remove the NR>1 then it works perfectly.

Many thanks for help.
  #6 (permalink)  
Old 07-10-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,869
NR > 1 excludes the first line:

Code:
src ip           dest ip     #of flows   total bytes
Perhaps you've already removed it?
Closed Thread

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0