Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating every other line with the next

Tags:

shell

sed

In a text document I want to concatenate every other line with the next. I guess sed is the thing to use? How would this be done?

like image 650
node ninja Avatar asked Jan 24 '12 13:01

node ninja


People also ask

How do you join two lines in sed?

sed operates by performing the following cycle on each lines of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; […]. Add a newline to the pattern space, then append the next line of input to the pattern space.

How do you join two lines in Shell?

In the two commands above, we passed two options to the paste command: -s and -d. The paste command can merge lines from multiple input files. By default, it merges lines in a way that entries in the first column belong to the first file, those in the second column are for the second file, and so on.

How do I append two lines in Unix?

Method # 1 – Using echo & Printf The simplest way to append multiple lines to a file is to use the echo and printf command. Let us start with echo. Echo is a command used to output a string or multiple strings as arguments.


1 Answers

This is easiest using paste:

paste -s -d' \n' input.txt  

Although there's a Famous Sed One-Liner (38) to emulate this as in potong's answer.

like image 151
Johnsyweb Avatar answered Oct 11 '22 19:10

Johnsyweb