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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate mode/ most common value

Status
Not open for further replies.

rhnaeco

Technical User
Aug 11, 2005
45
Hi, does anybody know how to calculate the mode using awk or sed?

I have a 3 column file and i need to find the most common value in the 3rd column.

thanks in advance

rhnaceo
 
post a sample input and a desired output, pls!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
example file:

a b 2
a b 2
a b 6
a b 2
a b 4
a b 2
a b 1

output:
mode=2

 
Code:
{
  a[$3]++
}
END {
    for(i in a)
         if (a[i] > freq) {most=i; freq=a[i] }
    printf("mode=%s\n", most)
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thank you vlad, most apprieciated.

rhnaeco
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top