Skip to content
Latchkey

sed a, i, c: Append, Insert, Change Lines

sed a appends a line after a match, i inserts before it, and c replaces the matched line entirely.

When you need to add whole lines rather than edit text inside them, a, i, and c are the tools. Their syntax is where GNU and BSD diverge most.

What it does

a queues text to print after the current line, i prints text before it, and c deletes the addressed line or range and prints replacement text instead. GNU sed accepts a one-line form a text; POSIX and BSD want a backslash then a newline before the text.

Common usage

Terminal
# GNU one-line form (ubuntu-latest)
sed '/^\[main\]/a enabled=true' config.ini
sed '2i # generated, do not edit' file
sed '/^debug=/c debug=false' app.properties

# POSIX / BSD portable form (works on macOS too)
sed '/^\[main\]/a\
enabled=true' config.ini

Options

CommandWhat it does
a textAppend text after the addressed line
i textInsert text before the addressed line
c textReplace the addressed line or range with text
a\ then newlinePOSIX/BSD portable form of append
leading whitespaceGNU strips leading blanks unless escaped with \

In CI

The GNU one-line a text and i text forms are convenient on ubuntu-latest but fail on BSD sed. For cross-platform jobs use the backslash-newline form, which both accept, or install gnu-sed on macOS and call gsed.

Common errors in CI

On macOS, sed '2a hello' file raises sed: 1: "2a hello": command a expects \ followed by text. On Linux a stray backslash can give sed: -e expression #1, char N: expected \ after a', c' or `i'. Match the form to the sed flavor.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →