bashsedstring-substitution

sed substitution variable containing "/"


I've got a file containing information: eg:

Hello
START
{
toto=1
tata=2
}
STOP

I need to substitute what is between START and STOP by the content of a bash variable.

This works well using sed \c but my variable contains the \ character that needs to be kept, and it's not the case.

my sed:

MY_VAR="it is 07\:00\:00"; sed -i "/START/,/STOP/c ${MY_VAR}" file 

result:

Hello
it is 07:00:00

expected:

Hello
it is 07\:00\:00

Thanks


Solution

  • I suggest to replace first all \ with \\\:

    MY_VAR="it is 07\:00\:00"
    MY_VAR="${MY_VAR//\\/\\\\}"
    sed "/START/,/STOP/c ${MY_VAR}" file
    

    Output:

    Hello
    it is 07\:00\:00