Search code examples
sedcygwin

Sed search and replace syntax in Cygwin?


I am trying to serch and replace using sed in Cygwin:

SRC_FOLDER_MAIN=bra
TGT_FOLDER_MAIN=tag

but the following sed command does not work:

sed 's/${SRC_FOLDER_MAIN}/${TGT_FOLDER_MAIN}/g' externals.txt > externals_mod.txt

This for example works OK (replaces "bra" with "tag"):

sed 's/bra/tag/g' externals.txt > externals_mod.txt

What am I missing?

Could it be that Cygwin is the problem?


Solution

  • if you need to pass shell variables in sed, you need to take them out of the single quote try like this

    sed 's/'${SRC_FOLDER_MAIN}'/'${TGT_FOLDER_MAIN}'/g' $externals.txt > $externals_mod.txt

    notice the ' just before and after ${SRC_FOLDER_MAIN} and similar in ${TGT_FOLDER_MAIN}

    Update: Example

    [[bash_prompt$]]$ cat log
    we will replace aaaa with bbbb
    [[bash_prompt$]]$ a=aaaa;b=bbbb
    [[bash_prompt$]]$ sed 's/'${a}'/'${b}'/g' log > new_log
    [[bash_prompt$]]$ cat new_log
    we will replace bbbb with bbbb