sedappenddouble-quotesprepend

sed prepend and add double-quote conditionnally


Playing with makefile and bash shell scripting where I ended-up having a variable containing:

build_ext --b2-args=libtorrent-python-pic=on --b2-args=address-model=32

I need to prepend --global-option= and append double-quotes at the right place such as:

--global-option=build_ext --global-option=--b2-args="libtorrent-python-pic=on"  --global-option=--b2-args="address-model=32"

Note that --b2-args= varies and cannot be taken as granted.

So far what I did for the prepending is using a bash array:

printf "\x2D\x2Dglobal-option=%s " "${myarray[@]}"

But I'd rather have a one-liner using sed instead.


Solution

  • Using any sed:

    $ sed 's/=\([^ ]*\)/="\1"/g; s/[^ ]*/--global-option=&/g' file
    --global-option=build_ext --global-option=--b2-args="libtorrent-python-pic=on" --global-option=--b2-args="address-model=32"