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

Append, Insert, and Change

The a, i, and c commands let sed add new lines after or before a matched line, or replace matched lines entirely, without relying on the substitute command.

Editing CommandsIntermediate8 min readJul 10, 2026
Analogies

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.

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

Change 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

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#AppendInsertAndChange#Append#Insert#Change#Adding#StudyNotes#SkillVeris#ExamPrep