Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to count and group records in AWK ?

Status
Not open for further replies.

cmartins

Technical User
Oct 16, 2001
8
BR
Hello

I'd like to count ($5) and group the records with commom Fields
($3 and $4) as follows:

Original text file:
-------------------
-
1000411075 833 192.168.39.25 TCP_MISS/300 525
1000411076 833 192.168.39.25 TCP_MISS/200 332
1000411077 61 192.168.39.25 TCP_HIT/200 583
1000411078 37 192.168.39.40 TCP_HIT/200 625
1000411077 54 192.168.39.40 TCP_MISS/200 325
1000411077 54 192.168.39.40 TCP_MISS/300 340

Text changed by AWK:
---------------------
192.168.39.25 TCP_MISS 2 occurrence(s) 857
192.168.39.25 TCP_HIT 1 occurrence(s) 583
192.168.39.40 TCP_HIT 1 occurrence(s) 625
192.168.39.40 TCP_MISS 2 occurrence(s) 665

Regards

Claudemir F. Martins
 

Hi cmartins!

Please, try this awk example:


Code:
/MISS/  {
    ind = $3 "   TCP/MISS"
    occ[ind] ++
    sum[ind] += $5 
}

/HIT/  {
    ind = $3 "   TCP/HIT"
    occ[ind] ++
    sum[ind] += $5 
}

END {
    for (i in occ)
        print i, occ[i], " occ. ", sum[i] 
}

As you see, I use arrays in this solution. Array is a powerful awk programming tool.

Instead print you can use printf function for better formatting.

Bye!

KP.
 
Good example krunek, this will also help me.
 

Thanks, marsd!

I'm glad to share my knowledge with all of you.

Good bless you! Bye!

KP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top