# frequency count using shell

**URL:** <https://community.unix.com/t/frequency-count-using-shell/317968>\
**Category:** Shell Programming and Scripting\
**Created:** [October 9, 2012, 4:51pm UTC](https://community.unix.com/t/frequency-count-using-shell/317968 "2012-10-09T16:51:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xshang](https://community.unix.com/letter_avatar/xshang/32/6_22734026db12803ebb3e059622ac3534.png) [@xshang](https://community.unix.com/u/xshang)\
**Post date:** [October 9, 2012, 4:51pm UTC](https://community.unix.com/t/frequency-count-using-shell/317968/1 "2012-10-09T16:51:46Z")

</div>

Hello everyone,  
please consider the following lines of a matrix

```auto
[574,] 59 32 
[575,] 59 32 
[576,] 59 32 
[577,] 59 32 
[578,] 59 32 
[579,] 59 32 
[580,] 59 32 
[581,] 60 32 
[582,] 60 33 
[583,] 60 33 
[584,] 60 33 
[585,] 60 33 
[586,] 60 33 
[587,] 60 33 
[588,] 60 33 
[589,] 60 33 
[590,] 60 33 
[591,] 61 33 
[592,] 61 33 
[593,] 61 33 
[594,] 61 33 
[595,] 61 33 
[596,] 61 33 
[597,] 61 33 
[598,] 61 33 
[599,] 61 33 
[600,] 61 33 
[601,] 62 34 

```

Is is possible to count the percent frequency of each distinct field in $2?

Just like this:

```auto
59 25.00%
60 35.70%
61 35.70%
62 3.57%

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 9, 2012, 5:01pm UTC](https://community.unix.com/t/frequency-count-using-shell/317968/2 "2012-10-09T17:01:50Z")

</div>

```nohighlight
awk '{ A[$2]++ } END { for(X in A) printf("%s\t%s\n", X, (A[X]*100)/NR) }' inputfile

```

---

<div class="post-metadata">

**Author:** ![fastlane3000](https://community.unix.com/letter_avatar/fastlane3000/32/6_22734026db12803ebb3e059622ac3534.png) [@fastlane3000](https://community.unix.com/u/fastlane3000)\
**Post date:** [October 9, 2012, 5:25pm UTC](https://community.unix.com/t/frequency-count-using-shell/317968/3 "2012-10-09T17:25:33Z")

</div>

in case you need to add "%" and use float

```nohighlight
awk '{ A[$2]++ } END { for(X in A) printf("%d\t%.2f%\n", X, (A[X]*100)/NR) }' inputfile

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [October 9, 2012, 5:30pm UTC](https://community.unix.com/t/frequency-count-using-shell/317968/4 "2012-10-09T17:30:55Z")

</div>

Same solution, added format:

```nohighlight
awk '{ A[$2]++ } END { for(X in A) printf("%s\t%5.2f%%\n", X, (A[X]*100)/NR) }' inputfile | sort -n

```

---

<div class="post-metadata">

**Author:** ![xshang](https://community.unix.com/letter_avatar/xshang/32/6_22734026db12803ebb3e059622ac3534.png) [@xshang](https://community.unix.com/u/xshang)\
**Post date:** [October 9, 2012, 5:37pm UTC](https://community.unix.com/t/frequency-count-using-shell/317968/5 "2012-10-09T17:37:17Z")

</div>

> [@fastlane3000](#):
>
> in case you need to add "%" and use float
> 
> ```plaintext
> awk '{ A[$2]++ } END { for(X in A) printf("%d\t%.2f%\n", X, (A[X]*100)/NR) }' inputfile
> 
> ```

It showed some message like this, how should I adjust the code? I'm not familiar with printf, Thank you!

```html
awk: weird printf conversion %

 input record number 61124, file HB143-0W-A4.txt
 source line number 1
awk: not enough args in printf(%d	%.2f%
)
 input record number 61124, file HB143-0W-A4.txt
 source line number 1

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [October 9, 2012, 5:44pm UTC](https://community.unix.com/t/frequency-count-using-shell/317968/6 "2012-10-09T17:44:28Z")

</div>

```nohighlight
awk '{ A[$2]++ } END { for(X in A) printf("%d\t%.2f%%\n", X, (A[X]*100)/NR) }' inputfile

```

---

<div class="post-metadata">

**Author:** ![fastlane3000](https://community.unix.com/letter_avatar/fastlane3000/32/6_22734026db12803ebb3e059622ac3534.png) [@fastlane3000](https://community.unix.com/u/fastlane3000)\
**Post date:** [October 9, 2012, 5:45pm UTC](https://community.unix.com/t/frequency-count-using-shell/317968/7 "2012-10-09T17:45:53Z")

</div>

maybe you're not using the same awk than i have (i've got a gnu version called gawk)  
just try "[rdrtx1](http://www.unix.com/members/302126107.html)"'s solution it's more complete with a "sort"

```plaintext
awk '{ A[$2]++ } END { for(X in A) printf("%s\t%3.2f%\n", X, (A[X]*100)/NR) }' inputfile | sort -n

```

i rather use 3 it's enough because we can't have more than 100%

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/6_22734026db12803ebb3e059622ac3534.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [October 9, 2012, 5:58pm UTC](https://community.unix.com/t/frequency-count-using-shell/317968/8 "2012-10-09T17:58:57Z")

</div>

Nit: %s makes more sense than %d since $2 (aka X) is treated as a string in every expression in which it appears.

Regards,  
Alister
