How to concatenate multiple lines of output to one line?

The Solution to How to concatenate multiple lines of output to one line? is


Use tr '\n' ' ' to translate all newline characters to spaces:

$ grep pattern file | tr '\n' ' '

Note: grep reads files, cat concatenates files. Don't cat file | grep!

Edit:

tr can only handle single character translations. You could use awk to change the output record separator like:

$ grep pattern file | awk '{print}' ORS='" '

This would transform:

one
two 
three

to:

one" two" three" 

~ Answered on 2013-03-22 21:31:18


Most Viewed Questions: