sed

Advanced sed commands

Insert a new line before matching pattern - using eXchange

Given a file file.txt with the following content:

line 1
line 2
line 3

You can add a new line using below command

sed '/line 2/{x;p;x;}' file.txt

The above command will output

line 1

line 2
line 3

Explanation:

x command is eXchange. sed has a buffer that you can use to store some lines. This command exchanges this buffer with current line (so current line goes to this buffer and buffer content becomes current line).

p command prints current line.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow