Often you may want to skip empty lines when using awk to perform various commands on a file.
The easiest way to do so is by using NF at the beginning of the awk command.
This stands for “number of fields” and will only perform commands on lines where the number of fields is greater than zero.
The following examples show how to use this syntax in practice with the following file named team_points.txt that contains information about various basketball players:

Example 1: Use awk to Skip Empty Lines when Printing Values
Suppose we use the following syntax to print all values from the first column of the file:
awk '{print $1}' team_points.txt
The following screenshot shows how to use this syntax in practice:

Notice that all values are printed, even the empty ones.
We could use NF to print all values from the first column and skip the empty values:
awk 'NF {print $1}' team_points.txt
The following screenshot shows how to use this syntax in practice:

Notice that awk now skips all empty lines when printing the values from the first column.
Example 2: Use awk to Skip Empty Lines when Counting Unique Values
Suppose we use the following syntax to count the number of occurrences of each unique value in the first column of the file:
awk '{print $1}' team_points.txt | sort | uniq -c
The following screenshot shows how to use this syntax in practice:

The results display the number of occurrences of each unique value in the column, even the number of occurrences of empty lines.
We could use NF to skip all empty lines when counting the occurrences of each value in the first column:
awk 'NF {print $1}' team_points.txt | sort | uniq -c
The following screenshot shows how to use this syntax in practice:

Notice that the number of occurrences of empty lines is no longer returned.
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
