How to merge every two lines into one from the command line? How to merge every two lines into one from the command line? bash bash

How to merge every two lines into one from the command line?


paste is good for this job:

paste -d " "  - - < filename


awk:

awk 'NR%2{printf "%s ",$0;next;}1' yourFile

note, there is an empty line at the end of output.

sed:

sed 'N;s/\n/ /' yourFile


Alternative to sed, awk, grep:

xargs -n2 -d'\n'

This is best when you want to join N lines and you only need space delimited output.

My original answer was xargs -n2 which separates on words rather than lines. -d can be used to split the input by any single character.