Turning multi-line string into single comma-separated

Alex Coplan picture Alex Coplan · Jan 3, 2012 · Viewed 135.4k times · Source

Let's say I have the following string:

something1:    +12.0   (some unnecessary trailing data (this must go))
something2:    +15.5   (some more unnecessary trailing data)
something4:    +9.0   (some other unnecessary data)
something1:    +13.5  (blah blah blah)

How do I turn that into simply

+12.0,+15.5,+9.0,+13.5

in bash?

Answer

Mattias Ahnberg picture Mattias Ahnberg · Jan 4, 2012

Clean and simple:

awk '{print $2}' file.txt | paste -s -d, -