Adding and Replacing Whole Lines
While the substitute command edits text within a line, the append (a), insert (i), and change (c) commands work at the level of whole lines. The append command queues text to be output after the current line; the insert command outputs text before the current line; and the change command deletes the current line (or range) and emits replacement text in its place. These commands are the standard way to add boilerplate, headers, footers, or full-line replacements addressed by line number or pattern.
Cricket analogy: Substitute is a batsman nudging a single within an over; append, insert, and change are the scoreboard operator adding a whole new line to the innings summary, inserting a note above it, or rewriting a mis-recorded over entirely.
Portable Syntax vs the GNU One-Liner
In classic POSIX sed the a, i, and c commands require a backslash followed by a newline, with the text on the following line(s) and each embedded newline escaped by a backslash. GNU sed adds a convenient one-line form where the text follows the command on the same line: a text, i text, or c text. The POSIX form is portable across BSD, macOS, and GNU; the one-line form is more readable but is a GNU extension. Leading whitespace in the appended text is stripped unless it is protected with a leading backslash.
Cricket analogy: The POSIX backslash form is the formal full-length lbw appeal to the umpire; the GNU one-liner is the quick shout every crowd understands. Both get the wicket, but only the formal one is recognised in every ground worldwide.
# GNU one-line form: append after line 2
sed '2a This line comes AFTER line 2' file.txt
# Insert BEFORE every line matching a pattern
sed '/^ERROR/i --- log entry ---' app.log
# Change (replace) all lines in a range
sed '3,5c REPLACED' file.txt
# Portable POSIX form (backslash + newline)
sed '/TODO/a\
reviewed 2026-07-10' notes.txtChange on Ranges Behaves Specially
The change command has a subtle behaviour when applied to an address range: rather than emitting the replacement text once for every line in the range, sed deletes all the lines in the range and outputs the replacement text a single time, at the end of the range. This differs from a and i, which act on each matched line individually. When c is given a single-line or pattern address that matches multiple separate lines, the text is emitted once per match, so understanding whether your address is a contiguous range or a repeated match is essential.
Cricket analogy: Change on a range is like a rain rule wiping out overs 3 through 5 and recording one revised total, not one entry per over; whereas append acts ball by ball, noting something after each delivery.
The c command applied to a range (e.g., 2,5c REPLACED) outputs the replacement text only ONCE, at the end of the range, and deletes all lines in between. If you expected the text to appear for each line, use a per-line address or the a/i commands instead. This range behaviour is a frequent source of surprise.
Because a, i, and c add or replace entire lines, they are ideal for injecting license headers, shebang lines, or configuration blocks. Combine them with a pattern address (e.g., /^import/i ...) to place text relative to matched content rather than fixed line numbers.
- a appends text after the current line; i inserts text before it.
- c deletes the current line or range and outputs replacement text.
- POSIX form uses backslash-newline; GNU sed allows a compact one-line form.
- The one-line a/i/c syntax is a GNU extension, not portable to BSD/macOS.
- c on a contiguous range emits the replacement text only once, at the range's end.
- a and i act on each matched line individually, unlike c on a range.
- Pattern addresses let you place text relative to matched content, not just line numbers.
Practice what you learned
1. What does the append command 'a' do?
2. How does the c command behave when applied to the range 2,5?
3. Which statement about the one-line 'a text' syntax is correct?
4. Which command would you use to add a line BEFORE every line starting with 'import'?
Was this page helpful?
You May Also Like
Substitute Command Flags
The flags that follow the sed substitute command control how many matches are replaced, whether matching is case-insensitive, and what happens after the replacement.
Reading and Writing Files
The r, R, w, and W commands let sed pull the contents of external files into the output stream and write matched lines out to separate files during a single pass.
The Hold and Pattern Space
sed maintains two buffers, the pattern space and the hold space, and a handful of commands move data between them to enable multi-line and stateful edits.
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