Insert blank line in UNIX after different number of lines

sed

Suppose I have a file abc.csv with below data:

abc
def
geh
ijk
lmn
opq
rst

Now, I want to insert blank lines after line2 and line 6.

Please suggest which command shall I use.

Best Answer

$ seq 10 | sed '2G;6G'
1
2

3
4
5
6

7
8
9
10

The G sed command appends a newline followed by the content of the hold space (here empty as we don't put anything in it) to the pattern space. So it's a quick way to add an empty line below that matched line.

Other alternatives are the a and s command:

sed '2a\

6a\
'

Or:

sed '2s/$/\
/
6s/$/\
/'

Some sed implementation also support:

sed '2s/$/\n/;6s/$/\n/'
Related Question