The insert and append commands

The insert, i, and append, a, commands work the same way with just a slight difference.

The insert command inserts the specified text before the specified line or pattern.

The append command inserts the specified text after the specified line or pattern.

Let's see some examples.

Our sample 02 file will be like this:

First line
Second line
Third line
Fourth line

To insert a line, you need to use the insert command i like this:

$ sed '2i\inserted text' myfile 

To append a line, you need to use the append command a like this:

$ sed '2a\inserted text' myfile 

Look at the result and check the inserted line position:

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.