Skip to content
Latchkey

awk NR: Number Lines and Limit Ranges

awk NR is the total count of records read so far, letting you number, select, or range over lines.

NR is how you grab line 1, skip a header, or print a window of lines without sed. It counts every record across every input file.

What it does

NR increments by one for each record (line) awk reads, never resetting between files. You use it as a pattern to select lines by position: NR==1 is the first line, NR>1 skips the header, NR>=10 && NR<=20 is a window.

Common usage

Terminal
# number every line
awk '{print NR, $0}' build.log
# print only line 42
awk 'NR==42' build.log
# everything except the header
awk 'NR>1' results.csv
# lines 10 through 20
awk 'NR>=10 && NR<=20' big.log

Patterns

ExpressionWhat it does
NRCurrent record number across all files
NR==1Match only the first line
NR>1Skip the first line (header)
NR%2==0Every even-numbered line
NR>=10 && NR<=20A range of lines (head/tail window)
END{print NR}Total line count after all input

In CI

awk 'END{print NR}' counts lines and is robust to a missing trailing newline, unlike some wc invocations. To stop early on a huge log, NR==N{print; exit} prints one line and quits without reading the rest.

Common errors in CI

NR keeps counting across multiple input files, so NR==1 only matches the very first file head, not each file. For per-file line numbers use FNR instead. A range like NR>10 NR<20 (no &&) is a syntax issue; combine conditions with && inside one pattern.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →