Binding Commands to One Address
Normally each sed command carries its own address, but often you want several commands to run on the same set of lines. Curly braces group commands so a single address governs the whole block: addr{ cmd1; cmd2; cmd3 }. sed evaluates the address once, and if it matches, every command inside the braces runs in order on that line's pattern space. Without braces you would have to repeat the address on each command, which is verbose and error-prone when the address is a complex regex.
Cricket analogy: Braces are like setting one field placement for a whole over: the captain decides the address once, and every ball in that over is bowled to the same plan without re-setting fielders.
Separators and Formatting
Inside braces, commands are separated by semicolons or newlines. GNU sed is lenient about spacing, but some commands are picky: the a, i, and c (append, insert, change) text commands and the label/branch commands historically require a newline or their own line, so mixing them with semicolons can fail on stricter seds. A closing brace should be preceded by a semicolon or newline. Writing multi-line scripts with real newlines, often via a -e per line or a script file with -f, is the most portable and readable approach for anything non-trivial.
Cricket analogy: Semicolons are the gaps between deliveries in an over; the a/c/i commands are like a bowler who needs a full run-up, so you must give them their own line rather than crowding them together.
# Run three commands on every line containing DEBUG
sed '/DEBUG/{
s/DEBUG/TRACE/
s/ */ /g
s/$/ <-- reviewed/
}' log.txt
# One-liner form with semicolons
sed '/^#/{ s/^#//; s/^ *//; }' config.txt
# Negated block: on lines OUTSIDE the range, do two edits
sed '/BEGIN/,/END/!{ s/foo/bar/; s/baz/qux/; }' file.txt
# Nested braces: within a range, act only on matching sub-lines
sed '10,20{ /KEY/{ s/=.*/=REDACTED/ } }' settings.txtFor scripts longer than a couple of commands, put them in a file and run sed -f script.sed. Real newlines sidestep every semicolon-versus-newline pitfall and make the logic reviewable in version control.
Nesting Blocks
Braces nest arbitrarily, letting you compose addresses like logical AND. The block 10,20{ /KEY/{ ... } } runs the inner commands only on lines that are both within lines 10-20 and match /KEY/. Each level re-tests its own address against the current pattern space, so nesting is the idiomatic way to combine a range restriction with a content filter. Every opening brace must have a matching close; an unbalanced brace produces an 'unexpected end of file' or 'unmatched {' error. Nesting depth is limited only by readability, not by sed itself.
Cricket analogy: Nested braces are a double condition like 'in the death overs AND facing a spinner': only deliveries meeting both filters trigger the aggressive shot, exactly as the inner block runs only when both addresses match.
Unbalanced braces are a frequent error: every { needs a matching }. If sed prints 'unexpected }'' or 'unmatched {''', count your braces. Semicolons or newlines before a closing } are required by some seds, so end blocks with '; }' or a newline.
- Braces group commands so one address governs them all: addr{ cmd1; cmd2 }.
- The address is tested once; if it matches, every command in the block runs in order.
- Commands inside braces are separated by semicolons or newlines.
- The a, i, c and label/branch commands often need their own line, so prefer newlines for them.
- Braces nest to combine addresses like logical AND, e.g. a range plus a content filter.
- Every { needs a matching }; unbalanced braces cause 'unmatched' errors, and a closing } should follow a separator.
Practice what you learned
1. What is the primary purpose of grouping commands with braces in sed?
2. How are commands separated inside a brace block?
3. What does 10,20{ /KEY/{ s/=.*/=X/ } } do?
4. Which commands commonly require a newline rather than a semicolon separator?
5. What error indicates a brace problem?
Was this page helpful?
You May Also Like
Negating Addresses
Using the ! operator to invert a sed address so a command runs on every line the address does NOT select, plus common idioms and gotchas.
The Print and Delete Commands
How sed's p (print) and d (delete) commands work, why -n changes everything, and the difference between d and D in the multi-line pattern space.
Line Ranges and Steps
Selecting spans of lines in sed using numeric ranges, the $ last-line symbol, regex-bounded ranges, and GNU's first~step notation for periodic selection.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics