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
# 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.iniOptions
| Command | What it does |
|---|---|
| a text | Append text after the addressed line |
| i text | Insert text before the addressed line |
| c text | Replace the addressed line or range with text |
| a\ then newline | POSIX/BSD portable form of append |
| leading whitespace | GNU 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.