0

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

markp-fuso
  • 28,790
  • 4
  • 16
  • 36
Totoc1001
  • 338
  • 2
  • 11
  • 1
    Try `sed -i "/START/,/STOP/c ${MY_VAR//'\'/'\\'}" file` – M. Nejat Aydin Dec 07 '22 at 14:00
  • 1
    `sed` itself has no concept of variables or accepting parameters. This is just an example of creating a `sed` script using *shell* parameter expansion. The script contains a literal `\:`, which `sed` itself resolves to `:`. – chepner Dec 07 '22 at 14:14

1 Answers1

2

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
Cyrus
  • 84,225
  • 14
  • 89
  • 153