Counting with sort and uniq

Here is another quick example of how to get a count of how many times a string appears. For my example I am going to use this output.

$ cat /etc/passwd | cut -d: -f7
/bin/bash
/bin/sh
/bin/sh
/bin/sh
/bin/sync
/bin/sh
/bin/sh
/bin/sh

These are the shells of users on my system, what if I wanted to see what the most common shell was?

$ cat /etc/passwd | cut -d: -f7 | sort | uniq -c | sort -nk1
1 /bin/sync
2 /usr/sbin/nologin
4 /bin/false
5 /bin/bash
20 /bin/sh

In order to get this result I take my output and send it to sort, this command will sort the output alphabetically. From there we pipe it to uniq -c which will find all of the unique occurrences of a string and print the number of times it shows up. We then sort by that number and BAM, we have a list of shells and how many users are using them.

About Benjamin

Benjamin is a Infrastructure and Software Engineer. On this blog he writes about Linux, Docker, Programming as well as other Systems topics.

Learn more about Linux

If you liked this article, check out Benjamin's book: Red Hat Enterprise Linux Troubleshooting Guide. Where you can learn a lot more about troubleshooting Linux systems. This book is filled with tips and techniques he has learned over years of managing mission critical systems.

Related