sed Append command Insert line after first match

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Given a file file.txt with the following content:

line 1
line 2
line 3

You can add a new line after first matching line with the a command.

For portable use the a command must be followed immediately by an escaped newline, with the text-to-append on its own line or lines.

sed '
/line 2/a\
new line 2.2
' file.txt
GNU sed

Some versions of sed allow the text-to-append to be inline with the a command:

sed '/line 2/a new line 2.2' file.txt

The above commands will output:

line 1
line 2
new line 2.2
line 3


Got any sed Question?