awk NR==1: Skip or Keep the Header Row
awk NR>1 processes every line except the first, the standard way to drop a header before parsing data.
Most tool output and CSV files lead with a header row you must skip before summing or filtering. NR is how you do it without sed or tail.
What it does
NR is the record number, so NR>1 is true for every line after the first, skipping a single header. NR==1 keeps only the header. To skip a header but only in the first file of several, gate on FNR>1 instead.
Common usage
# sum a column, skipping the header
awk 'NR>1 {sum += $2} END {print sum}' data.csv
# print just the header
awk 'NR==1' data.csv
# skip the header in each file (multi-file)
awk 'FNR>1' *.csv
# alternative: skip the header line explicitly
awk 'NR==1 {next} {print $1}' data.csvIdioms
| Snippet | What it does |
|---|---|
| NR>1 | Process all lines after the first |
| NR==1 | Keep only the header line |
| NR==1{next} | Skip the header, then fall through to the main block |
| FNR>1 | Skip the header of each file in a multi-file run |
| NR>2 | Skip a two-line header |
In CI
When you concatenate several CSVs (awk ... *.csv), NR>1 only drops the very first header and keeps the rest as data; use FNR>1 to drop each file header. Pair NR>1 with a column sum so a header label never coerces to 0 and skews a total.
Common errors in CI
Using NR>1 across multiple files keeps every header except the first, polluting the data; switch to FNR>1. A two-line header needs NR>2, not NR>1. Putting NR==1{next} after the main block runs the main block on the header first; place the skip rule before it.