[awk] Count fields on each line with NF

Created on November 30, 2026

Use NF to get the number of fields in the current record. Save this sample as fruits.txt.

apple red 120
banana yellow 80
grape purple 300

This command prints one count per input line.

awk '{print NF}' fruits.txt
3
3
3

With the default field separator, runs of whitespace separate the fields.

awk Shell command