How to Group By and Count Lines in Bash (With Example)


Often you may want to use Bash to group by and count lines in a file.

You can use the following syntax to do so:

sort cities.txt | uniq -c | sort -nr | awk '{ print $2, $1 }'

This particular example will return the total count of each unique line in the file named cities.txt.

Here is what the various commands do:

  • sort cities.txt: Sorts the lines in the file
  • uniq -c: Returns the count of each unique line
  • sort -nr: Sorts by count numerically in descending order
  • awk ‘{ print $2, $1 }’: Prints the line first, then the count second

The following example shows how to use this syntax in practice.

Example: How to Group By and Count Lines in a File in Bash

Suppose that we have a file named cities.txt in our current directory that contains the names of various U.S. cities.

We can use the cat  command to view the contents of this file:

Suppose that we would like to count the number of times that each unique city name occurs in the file.

We can use the following syntax to do so:

sort cities.txt | uniq -c | sort -nr | awk '{ print $2, $1 }'

The following screenshot shows how to use this syntax in practice:

Bash count lines by group

This returns the count of each unique line in the file.

For example, we can see:

  • The line with the text Dallas occurs 3 times in the file.
  • The line with the text Miami occurs 2 times in the file.
  • The line with the text Sacramento occurs 1 time in the file.
  • The line with the text Milwaukee occurs 1 time in the file.

And so on.

Note: If you would prefer the count to appear first and the city name to appear second in the output then you can simply leave out the last pipe that contains the awk command.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to List All Files in Folder by Size
Bash: How to List All Files in Folder Without Extension
Bash: How to List All Files Matching a Specific Pattern

Leave a Reply