awksedtail

How to add N blank lines between all rows of a text file?


I have a file that looks

a
b
c
d

Suppose I want to add N lines (in the example 3, but I actually need 20 or 100 depending on the file)

a


b


c


d 

I can add one blank line between all of them with sed

sed -i '0~1 a\\' file

But sed -i '0~3 a\\' file inserts one line every 3 rows.


Solution

  • You may use with GNU sed:

    sed -i 'G;G;G' file
    

    The G;G;G will append three empty lines below each non-final line.

    Or, awk:

    awk 'BEGIN{ORS="\n\n\n"};1'
    

    See an online sed and awk demo.

    If you need to set the number of newlines dynamically use

    nl="
    "
    awk -v nl="$nl" 'BEGIN{for(c=0;c<3;c++) v=v""nl;ORS=v};1' file > newfile