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

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.

Benjamin Cane
Principal Engineer, Vice President

Principal Engineer focused on building cloud-native software solutions for financial platforms. Benjamin uses both his systems and software knowledge to build end-to-end platforms. Platforms purpose built for performance, and resiliency.