Capturing Parts of a Match
A group in a sed regular expression captures a portion of the match so you can refer to it later. In basic regular expressions (BRE, sed's default) groups are written with escaped parentheses '\(' and '\)'; with the -E or -r flag for extended regular expressions (ERE) you use plain '(' and ')'. Each group is numbered left to right by the position of its opening parenthesis, so the first group is 1, the second is 2, and so on, up to nine.
Cricket analogy: A group is like tagging a specific fielder's position on the field diagram so the commentator can reference 'the man at gully' again later without re-describing where he stands.
Backreferences in the Replacement
In the replacement text of an 's' command, '\1' through '\9' insert the text captured by the corresponding group, and '&' inserts the entire matched text. This lets you rearrange or reformat data: for example swapping 'Last, First' into 'First Last' by capturing each name and emitting them in reverse. The '&' is especially handy for wrapping a whole match, such as surrounding every matched number with brackets without retyping it.
Cricket analogy: Using '\1' and '\2' to swap names is like reversing the batting order on the scorecard — the same two players, just repositioned in the line-up.
Backreferences Inside the Pattern
Backreferences also work inside the pattern itself, where '\1' matches the exact text that group 1 already captured — not the pattern, but the literal string it matched. This is how you find repeated words or doubled characters: '\(\w\+\) \1' matches any word immediately repeated. Note this is a genuine regex feature enabling matches that plain regular languages cannot express, so it is powerful but can be slower on large inputs.
Cricket analogy: An in-pattern backreference is like spotting a bowler send down two identical deliveries back to back — you are matching the repeat of the very same ball, not just any ball.
Reformatting With Groups
# Swap 'Last, First' into 'First Last' (BRE, default sed).
echo 'Curie, Marie' | sed 's/\(.*\), \(.*\)/\2 \1/'
# Output: Marie Curie
# Same task with extended regex (no backslashes on the parens).
echo 'Curie, Marie' | sed -E 's/(.*), (.*)/\2 \1/'
# Wrap every whole number in brackets using &.
echo 'order 42 line 7' | sed -E 's/[0-9]+/[&]/g'
# Output: order [42] line [7]
# Collapse doubled words using an in-pattern backreference.
echo 'the the cat' | sed -E 's/\b(\w+) \1\b/\1/g'
# Output: the catsed supports groups 1 through 9. If you need to reference a captured group but also want to keep the numbering simple, remember that groups are counted by the order of their opening parenthesis, left to right, even when groups are nested.
In basic regular expressions you MUST escape the parentheses as '\(' and '\)' and the plus as '\+'; in extended regex ('-E' or '-r') you must NOT escape them. Mixing the two conventions is the single most common sed regex bug — 's/(foo)/\1/' silently matches a literal parenthesis in BRE rather than creating a group.
- Groups capture parts of a match: '\(...\)' in BRE, '(...)' in ERE (-E/-r).
- Groups are numbered 1-9 by the position of their opening parenthesis.
- In the replacement, '\1'-'\9' insert captured text and '&' inserts the whole match.
- Backreferences in the pattern ('\1') match the exact text a group captured — great for finding repeats.
- BRE requires escaping parentheses; ERE requires plain parentheses — do not mix them.
- '&' is ideal for wrapping or duplicating an entire match without retyping it.
- Backreference matching is powerful but can be slower on large inputs.
Practice what you learned
1. In sed's default basic regular expressions, how do you create a capturing group?
2. What does '&' represent in the replacement text of an 's' command?
3. What does a backreference like '\1' match when used inside the pattern?
4. How are capturing groups numbered in sed?
5. Why does 's/(foo)/\1/' fail to capture in default sed?
Was this page helpful?
You May Also Like
In-Place Editing
Learn how sed's -i option edits files directly on disk, how to create backups, and the portability differences between GNU and BSD/macOS sed.
Multiline Processing
Learn how sed handles multiple lines at once using the pattern space, the hold space, and the D, P, N, H, and G commands to perform edits that span line boundaries.
The n and N Commands
Understand how sed's lowercase n and uppercase N commands pull additional input lines into processing, and how they differ in what they do to the pattern space.
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