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

shantanuo picture shantanuo · Mar 7, 2012 · Viewed 241.9k times · Source

I have a text file with the following format. The first line is the "KEY" and the second line is the "VALUE".

KEY 4048:1736 string
3
KEY 0:1772 string
1
KEY 4192:1349 string
1
KEY 7329:2407 string
2
KEY 0:1774 string
1

I need the value in the same line as of the key. So the output should look like this...

KEY 4048:1736 string 3
KEY 0:1772 string 1
KEY 4192:1349 string 1
KEY 7329:2407 string 2
KEY 0:1774 string 1

It will be better if I could use some delimiter like $ or ,:

KEY 4048:1736 string , 3

How do I merge two lines into one?

Answer

glenn jackman picture glenn jackman · Mar 7, 2012

paste is good for this job:

paste -d " "  - - < filename