Artificial intelligent assistant

Replace iteratively with things contained in array foo bar, foo bar, foo bar foo bar Given this beautiful poem, how could all the `bar` be replaced by `superman`, but each time with a different ending, contained in an array `(is super, is blazing fast, is marvelous, cawabanga)` So the result would be : foo superman is super, foo superman is blazing fast, foo superman is marvelous foo superman cawabanga I know it is possible to use a for/loop to do so, with sed. But is there a shorter, more beautiful solution?

You can do this just with shell parameter expansion: It's really a one-liner


poem='foo bar, foo bar, foo bar
foo bar'
traits=( "is super" "is blazing fast" "is marvelous" "cawabanga" )

for trait in "${traits[@]}"; do poem=${poem/bar/"superman $trait"}; done

echo "$poem"



foo superman is super, foo superman is blazing fast, foo superman is marvelous
foo superman cawabanga

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 70086c8cd2ee23daaf2058d7e9942b3c