Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

sed/bash/wharever :D

INPUT:

sadf
asdf
yxcv
cxv
eqrt
asdf

OUTPUT:

sadf
asdf

yxcv
cxv

eqrt
asdf
share|improve this question

2 Answers 2

up vote 14 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}"
    # Based on @manatwork's comment
    ((lines++ % 2)) && echo
done < "$1" 
share|improve this answer
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
    
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

Another awk flavour:

awk '{ l=$0; getline; printf("%s\n%s\n\n", l, $0) }'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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