Prabhat Ratnala
Prabhat Ratnala

Reputation: 705

Enclose a word with other text using sed

I am trying append and prepend some text to each word using sed

My input is:

abc, def

I am expecting the output to be:

cast(abc as string), cast(def as string)

I am trying to do something similar to this:

test='abc, def'
echo "${test}"|sed 's/\</cast(a./g'|sed 's/\>/as string/g'

but this is giving something little different than expected

castas string(aas string.abcas string, castas string(aas string.defas string

Upvotes: 1

Views: 114

Answers (2)

mop
mop

Reputation: 433

test='abc, def'
echo "${test}"|sed -r 's/\w+/cast(& as string)/g'

Upvotes: 1

anubhava
anubhava

Reputation: 785866

You can use awk:

awk 'BEGIN{FS=OFS=", "} {for (i=1; i<=NF; i++) $i = "cast(" $i " as string)"} 1' <<< "$test"

cast(abc as string), cast(def as string)

Upvotes: 1

Related Questions