Simplifying sed/tr

Hi all, I don't have much experience with shell scripting and I was wondering if there's a shorter way to write this.

Basically, given a list of strings separated by new lines, I want to prepend each string with a prefix and separate the strings with commas
i.e.

stra
strb
strc

becomes

prefix:stra,prefix:strb,prefix:strc

The command I'm currently using is:

cat input | sed 's/^/'text:'/' | tr '\n' ',' | sed 's/,$//g'

This seems a little wordy though for something this simple. Any ideas?

Thanks!

sed -e 's/^/prefix:/' myFile| sed -e :a -e '$!N;s/ *\n/,/;ta' |  sed 's/,$//'
or
awk '{$0="prefix:"$0;s=(s)?s","$0:$0};END{print s}' myFile
1 Like
sed 's/^/prefix:/' input | paste -sd, -

Regards and welcome to the forum,
Alister

2 Likes

Thanks guys! That definitely looks much better.