How can I use text-processing tools to insert a new line after every N lines?

Example for N=2:

INPUT:

sadf
asdf
yxcv
cxv
eqrt
asdf

OUTPUT:

sadf
asdf

yxcv
cxv

eqrt
asdf
up vote 23 down vote accepted

With awk:

awk ' {print;} NR % 2 == 0 { print ""; }' inputfile

With sed (GNU extension):

sed '0~2 a\\' inputfile

With bash:

#!/bin/bash
lines=0
while IFS= read -r line
do
    printf '%s\n' "${line}"
    ((lines++ % 2)) && echo
done < "$1"
  • 2
    Arithmetic evaluation can be used directly as condition, no need for the [[ ]] to test: while read line; do echo "$line"; ((lines++ % 2)) && echo; done. – manatwork Oct 29 '11 at 14:57
  • 1
    Be aware that the above documented command sed '0~2 a\ ' adds a space to each inserted newline. If you wanted to add a newline after each line, any of these similarly work: sed '0~1 a\ ', sed 'a\ ', or just sed G. – A-B-B Nov 7 '13 at 19:20

Using paste

 paste -d'\n' - - /dev/null <file
sed n\;G <infile

... is all you need ...

For example:

seq 6 | sed n\;G

OUTPUT:

1
2

3
4

5
6

...(and a blank follows the 6 as well)...or...

seq 5 | sed n\;G

OUTPUT:

1
2

3
4

5

(and no blank follows the 5)

If a blank should always be omitted in a last line case:

sed 'n;$!G' <infile

Another awk flavour:

awk '{ l=$0; getline; printf("%s\n%s\n\n", l, $0) }'
  • 1
    "This answer was flagged as low-quality because of its length and content." I'll accept it... ;-) – Hauke Laging Jan 27 '15 at 10:31

sed (GNU)

With (GNU) sed:

sed '0~2G'

Short (ugly for N=100):

sed 'n;G'

man sed explains ~ as:

first ~ step
Match every step'th line starting with line first. For example, ``sed -n 1~2p'' will print all the odd-numbered lines in the input stream, and the address 2~5 will match every fifth line, starting with the second. first can be zero; in this case, sed operates as if it were equal to step. (This is an extension.)

sed (other)

With other sed (Count new lines):

sed -e 'p;s/.*//;H;x;/\n\{2\}/{g;p};x;d'

Or, to be more portable, written as (remove comments for some versions of sed) :

sed -e '             # Start a sed script.
         p            # Whatever happens later, print the line.
         s/.*//       # Clean the pattern space.
         H            # Add **one** newline to hold space.
         x            # Get the hold space to examine it, now is empty.
         /\n\{2\}/{   # Test if there are 2 new lines counted.
             g        # Erase the newline count.
             p        # Print an additional new line.
           }          # End the test.
         x            # match the `x` done above.
         d            # don't print anything else. Re-start.
       '              # End sed script.

awk

With awk, probably:

awk '1 ; NR%2==0 {printf"\n"} '

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.