sedawk

Concatenate Lines in Bash


Most command-line programs just operate on one line at a time.

Can I use a common command-line utility (echo, sed, awk, etc) to concatenate every set of two lines, or would I need to write a script/program from scratch to do this?

$ cat myFile
line 1
line 2
line 3
line 4

$ cat myFile | __somecommand__
line 1line 2
line 3line 4

Solution

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

    Grab next line, and substitute newline character with space.

    seq 1 6 | sed 'N;s/\n/ /;'
    1 2
    3 4
    5 6