Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to concatenate lines into one string

Tags:

bash

shell

I have a function in bash that outputs a bunch of lines to stdout. I want to combine them into a single line with some delimiter between them.

Before:

one 
two 
three 

After:

one:two:three   

What is an easy way to do this?

like image 247
Shep Avatar asked Apr 24 '12 18:04

Shep


People also ask

How do I concatenate a new line in a string?

\n Seems to do the trick for you.

How do you combine five lines contents into single line in Unix?

The -s option can let it merge lines row-wise. Also, we told the paste command to separate merged lines using a given delimiter character by passing -d ” or -d ','.


1 Answers

Use paste

$ echo -e 'one\ntwo\nthree' | paste -s -d':'
one:two:three
like image 119
Daenyth Avatar answered Nov 15 '22 21:11

Daenyth