stuck on first script with sed issue

Hi,

I'm developing my first bash script and have made good progress but stuck at this point.

I've run sed on a text file to extract some data and have saved it into a variable e.g.

$blah

the variable contains emails as follows e.g.

<a@b.com> <b@c.com>

I'm now trying to edit the values in this variable so that if there is more than one email address, then they should be separated by commas instead of spaces.

I've currently got this far:

#$num is how many words there are in the variable
if (($num==1))
then
#insert sed command to put single mail address in ok (i will add this later)
else
#below i'm trying to substitute the spaces for commas but it's only adding #a comma to the last email address
blahnew=$(echo $blah | sed 's/[^ ].*/&,/g')
echo $blahnew
fi

so i get:

 <a@b.com> <b@c.com>

,

Would appreciate any help please.

Just change the space to comma:

NewBlah=$(echo $blah | sed 's/ /,/')
1 Like

Hi, that does seem like a great idea but it didn't work when I tried it, it left the spaces in. :confused:

---------- Post updated at 03:06 PM ---------- Previous update was at 03:01 PM ----------

Ah great, it did work!

it just needed the g flag at the end.! :slight_smile: