100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Grouping Commands with Braces

Using { } to bind multiple sed commands to a single address so they execute together as a block, including nesting and separator rules.

Patterns & AddressingIntermediate9 min readJul 10, 2026
Analogies

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.

bash
# 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.txt

For 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

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#GroupingCommandsWithBraces#Grouping#Commands#Braces#Binding#StudyNotes#SkillVeris#ExamPrep