How do I insert a blank line every n lines using awk?

frenk picture frenk · Jun 10, 2009 · Viewed 31.8k times · Source

I've got an input file like this:

line 1
line 2
line 3
line 4
line 5
line 6

I'd like to use awk to insert a blank line every few lines; for example, every two:

line 1
line 2

line 3
line 4

line 5
line 6

How can I get awk to put a blank line into my file every n lines?

Answer

glenn jackman picture glenn jackman · Jun 11, 2009

A more "awk-ish" way to write smcameron's answer:

awk -v n=5 '1; NR % n == 0 {print ""}'

The "1;" is a condition that is always true, and will trigger the default action which is to print the current line.