[awk] Count the lines in a file

Created on November 30, 2026

The built-in variable NR counts records read so far. With the default record separator, each line is a record. Suppose fruits.txt contains these lines.

apple
orange
banana

Print NR in an END block to report the total after awk reads the entire file.

awk 'END {print NR}' fruits.txt

Output

3
awk Shell command