Skip to content
Latchkey

awk next: Skip Lines and Stage Rules

awk next abandons the rest of the rules for the current line and reads the next record, so a guard rule can skip lines cleanly.

next is how you say "ignore this line and move on" without nesting every later rule in an if. It pairs perfectly with the NR==FNR file-join idiom.

What it does

next immediately stops processing the current record and fetches the next one, skipping all rules below the one that called it. It is the standard way to discard headers, blank lines, or comment lines before the main logic runs. nextfile (gawk) skips the rest of the current file.

Common usage

Terminal
# skip comment lines, process the rest
awk '/^#/ {next} {print $1, $2}' config.txt
# skip the header, then the main block runs on data only
awk 'NR==1 {next} {sum += $2} END {print sum}' data.csv
# the file-join idiom relies on next
awk 'NR==FNR {keys[$1]; next} $1 in keys' allow.txt data.txt
# skip blank lines
awk '/^[[:space:]]*$/ {next} {print}' input.txt

Statements

StatementWhat it does
nextSkip remaining rules, read the next record
/^#/{next}Skip comment lines before later rules
NR==1{next}Skip the header line
nextfilegawk: skip the rest of the current file
{...; next} (in first-file block)Stop first-file lines falling into later rules

In CI

A leading guard like /^#/{next} or /^$/{next} cleans a config or log of comments and blanks before the parsing rules, which is tidier than wrapping everything in a condition. In the NR==FNR join, the next after the first-file block is what keeps those lines out of the second-file logic.

Common errors in CI

next inside a BEGIN or END block is an error ("next used in BEGIN or END action" in gawk); it only makes sense in a per-record rule. Forgetting next in a guard rule lets the skipped line also run later rules, so a comment line gets parsed as data. nextfile is gawk-only and errors on mawk or BSD awk; guard for portability.

Related guides

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