How to combine lines in one line

I have a function in bash that prints a bunch of lines in stdout. I want to combine them in one line with some separator between them.

Before:

one 
two 
three 

After:

one:two:three   

What is an easy way to do this?

+3
source share
6 answers

Use paste

$ echo -e 'one\ntwo\nthree' | paste -s -d':'
one:two:three
+8
source

This might work for you:

paste -sd':' file
+2
source

:

cat file | tr -s "\n" ":"
+2

@glennJackman

awk '{printf("%s%s", sep, $0); sep=":"} END {print ""}' file

, bash

while read line ; do printf "%s:" $line ; done < file | sed s'/:$//'

,

+1

bash -:

echo $'one\n2 and 3\nfour' | { mapfile -t lines; IFS=:; echo "${lines[*]}"; }

one:2 and 3:four

{} , , , . .

http://www.gnu.org/software/bash/manual/bashref.html#index-mapfile-140

+1

input.txt

one
two
three

Perl : dummy.pl

 @a = `cat /home/Input.txt`;
    foreach my $x (@a)
    {
            chomp($x);
            push(@array,"$x");
    }
    chomp(@array);
    print "@array";

script :

$> perl dummy.pl | sed 's //: / g'> Output.txt

output.txt

one two Three

0
source

All Articles