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.

Avatar
Benjamin Cane
Principal Engineer, Vice President

Benjamin Cane is Principal Engineer at American Express. He has more than 16 years of experience with roles in both systems and software engineering. He leverages both his systems and software skills to build end-to-end platforms. Platforms, purpose built for performance and resiliency. Benjamin is also the author of Red Hat Enterprise Linux - Troubleshooting Guide (2015, Packt Publishing), and he has published many popular articles on topics such as Linux, Docker, Python, Go and Performance Tuning. Thoughts and Opinions expressed in my articles are my own.

Next
Previous

Related