Instead of the "sed" in the original question, I have written a simpler one with echo to illustrate the idea: Instead of looping through the whitespace separated items:
First we get the word count after "," substitution to determine the number of iterations
Convert the whitespaced string to an array with "()"
Iterate through counts
Make " or" part an output of a conditional and into a variable
Subset the array with iteration
And echo/sed, etc
#!/bin/bash H="host1,host2,host3" H2=${H//,/ } N=`echo $H2 | wc -w` H3=($H2) for ((i in= $(seq1; 1i 1<= $N$N; i++)) do if [ "$i" -eq "$N" ]; then OR=""; else OR=" or"; fi ITEM=${H3[$i-1]} echo "$ITEM$OR" done