In awk, NF stands for Number of Fields. It represents the total number of columns (fields) in the current line being processed.
Here are the most common ways to use NF:
1. Accessing the Last Column
Since NF is the total count of fields, $NF (with a dollar sign) refers to the value of the very last field, regardless of how many columns are in the line.
# Prints the last column of every line
awk '{print $NF}' awk_test.txt
2. Filtering by Column Count
You can use NF to find lines that have a specific number of data points. For example, to find lines that are missing data:
# Only prints lines that have exactly 3 columns
awk 'NF == 3' awk_test.txt
3. Processing Variable Data
If you have a file where some lines have more words than others, NF helps you handle them dynamically.
# Prints how many words (fields) are on each line
awk '{print "Line " NR " has " NF " fields"}' awk_test.txt
Summary of the difference:
NR: Number of Records (the current Row number).NF: Number of Fields (the count of Columns in the current row).$NF: The content of the last column.
Would you like to try running a command using NF on your awk_test.txt to see how it works?