Awk to count matching IP address


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk to count matching IP address
# 1  
Old 05-27-2008
Awk to count matching IP address

Hi all,

I am kinda new to unix and shell programming. I am stucked with the raw data to do the filtering things. Generally, the output should count unique connected destination for each source and total of flows for it. Here is the example of input file:
Code:
  Source IP               Dest IP           No. of Flows
222.233.123.136   192.168.129.197      9
221.241.57.125    192.168.129.197      22
221.241.57.125    192.168.128.210       1
221.187.240.145   192.168.129.197     6
221.128.134.247   192.168.129.197     18
221.128.134.247   192.168.129.1        20

The output should be like this format
Code:
222.233.123.136   1     9
221.241.57.125     2    23
221.187.240.145    1    6
221.128.134.247   2    38

I am using awk for this job. Really need for help. Thanks in advance
Here is the piece of my coding.
Code:
awk 'BEGIN{SUBSEP="@"}
	
	count=0
 	{ for(i=1;i < NF;i++) 
	{ if ($i==$1)
		{ 
		src=$1
		conn=$3
		count++
		}
	}
	{
	if ($1==src)
	{
	while (count>1)
	{ conn += $3 }
	}	
	}
		unique[src,conn]++
}
	
			
END{
	for (i in unique){
	n=split(i,u,SUBSEP)
	print u[1]"   " unique[i]"  "u[2]
 
}

}' tcpfile.sorted | sort -rn >> tcpfile.unique
}


Last edited by Yogesh Sawant; 05-27-2008 at 08:22 AM.. Reason: added code tags
# 2  
Old 05-27-2008
Try:
Code:
awk 'NR>1{a[$1]++;b[$1]=b[$1]+$3}END{for (i in a) print i,a[i],b[i]}' file|sort -rn

# 3  
Old 05-27-2008
Hi Klashxx,

NR>1 condition is not needed i think . Infact it should not be there

Thanks
Penchal
# 4  
Old 05-28-2008
It's work

Thanks you very much for your help.
It's working now.
Cheers.. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Compare two files and count number of matching lines

Dear All, I would like to compare two files and return the number of matches found. Example File A Lx2 L1_Mus1 L1Md_T Lx5 L1M2 L1_Mus3 Lx3_Mus Lx9 Lx2A L1Md_A L1Md_F2 File B L1_Mus3 L1_Mus3 (3 Replies)
Discussion started by: paolo.kunder
3 Replies

3. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

4. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

5. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

6. Shell Programming and Scripting

awk: matching and not matching

Hello all, simple matching and if not match problem that i can't figure out. file1 hostname: 30 10 * * * /home/toto/start PROD instance_name1 -p 00 9 * * * /home/toto/start PROD instance_name2 -p 15 8 * * * /home/toto/start PROD instance_name3 -p hostname2: 00 8 * * *... (5 Replies)
Discussion started by: maverick72
5 Replies

7. Shell Programming and Scripting

count numbers of matching rows and replace its value in another file

Hello all, can you help me in this problem, assume We have two txt file (file_1 and file_3) one is file_1 contains the data: a 0 b 1 c 3 a 7 b 4 c 5 b 8 d 6 . . . . and I need to count the lines with the matching data (a,b,..) and print in new file called file_2 such as the... (4 Replies)
Discussion started by: GoldenFalcon10
4 Replies

8. Shell Programming and Scripting

Count matching filenames in a folder

Hi all, I have 4 files for example named abc01012009.txt abc02012009.txt abc03012009.txt abc04012009.txt in a folder. I would like to firstly backup the latest file available, in this case, the latest date available, abc04012009.txt to its subfolder named backup, and then rename the... (4 Replies)
Discussion started by: tententen
4 Replies

9. Shell Programming and Scripting

Pattern Matching Count Urgent

The input is like NO Code 030019991 22 030087819 0 030089001 22 030178901 39 030333333 22 Patterns i want to match is 0300 0301 0303. i want to count different Codes occurances for each of these pattern. for example for 0300 Code 22... (6 Replies)
Discussion started by: krabu
6 Replies

10. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies
Login or Register to Ask a Question