0

i have special characters in the content of $VALUE which I would to add in file ($FILE).

When I run this cammand :

sed -i "s|T_VALUE|$VALUE|" "$FILE"

i have an error unterminated s command

i saw some post saying i need to escape the special characters, but i do not know them upfront as $VALUE is a dynamic value. how can i solve this?

Romeo Ninov
  • 16,541
  • 5
  • 32
  • 44
akaliza
  • 101
  • 1
  • it did not help because I am still getting the error, any example that escape all the special character? – akaliza Mar 15 '23 at 14:32
  • 1
    Use other delimiter, in this case pipe do other things – Romeo Ninov Mar 15 '23 at 15:03
  • See [is-it-possible-to-escape-regex-metacharacters-reliably-with-sed](https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed). – Ed Morton Mar 16 '23 at 00:42

1 Answers1

2

You can use Perl for fancier quoting. To avoid confusion, you can pass arguments via environment variables.

$ SUB_LH='?|'
$ SUB_RH='/\'
$ cat mydata.txt
x?|y
?|ab?|
$ SUB_LH=$SUB_LH SUB_RH=$SUB_RH perl -p -i.bak -e 's/\Q$ENV{SUB_LH}\E/$ENV{SUB_RH}/g' mydata.txt
$ cat mydata.txt
x/\y
/\ab/\
don_aman
  • 1,353
  • 3
  • 9