How to Use awk to Count Unique Values in Column


Often you may want to use awk to count the number of unique values in a specific column of a file.

You can use the following basic syntax to display the unique values in a column:

awk '{print $2}' athlete_data.txt | sort | uniq

This particular example will display each unique value in column 2 of the file named athlete_data.txt.

And you can use the following syntax to display the count of unique values in a column:

awk '{print $2}' athlete_data.txt | sort | uniq | wc -l

This particular example will display the count of unique values in column 2 of the file named athlete_data.txt.

The following example shows how to use each of these methods in practice.

Example: How to Use awk to Count Unique Values in Column

Suppose that we have the following file named athlete_data.txt that contains information about various basketball players:

We can use the following syntax to display each of the unique values in column 2 of the file:

awk '{print $2}' athlete_data.txt | sort | uniq

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

awk get unique values in column

From the output we can see that there are three unique values in column 2:

  • Center
  • Forward
  • Guard

If we’d like, we can use the following syntax to simply return the count of unique values in the column:

awk '{print $2}' athlete_data.txt | sort | uniq | wc -l

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

awk count unique values in column

The output tells us that there are 3 unique values in column 2 of the file.

Related Tutorials

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

How to Use awk to Print All Columns After Specific Number
How to Use awk to Print Rows Where Column Equals Value
How to Use awk to Print Last Line of a File
How to Use awk to Print a Range of Columns

Leave a Reply