Skip to content
Latchkey

awk Range Patterns: From One Match to Another

awk /start/,/end/ matches the block of lines beginning at the first pattern and ending at the second, inclusive.

Pulling a stack trace or one test suite section out of a long log is what range patterns are for. The range is inclusive on both ends.

What it does

A range pattern is two patterns separated by a comma. It turns on when the first pattern matches and stays on through the line where the second matches, including both boundary lines. The action runs for every line in the range.

Common usage

Terminal
# from BEGIN TRACE to END TRACE, inclusive
awk '/BEGIN TRACE/,/END TRACE/' app.log
# a section by line numbers
awk 'NR==10, NR==20' big.log
# extract a YAML block and print it
awk '/^jobs:/,/^[^[:space:]]/' workflow.yml

Forms

FormWhat it does
/a/,/b/Lines from the first /a/ match through the next /b/ match
NR==5,NR==9Lines 5 through 9 by record number
/start/,0From the first match to end of input (0 never matches as end)
range {action}Run an action on every line in the range

In CI

Range patterns extract a failing section from a verbose log without counting lines. If the end pattern can recur, the range re-arms and matches multiple blocks, which is often what you want for repeated trace markers.

Common errors in CI

If the end pattern never matches, the range runs to end of input, capturing far more than intended; verify the end marker exists. The range is inclusive, so both the /start/ and /end/ lines appear in the output; trim them with NR or an extra condition if you want only the interior. A single pattern with a comma typo can read as two arguments to awk.

Related guides

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