Passa a variável doublequoted como um argumento para Perl, ele pode manipular caracteres especiais em variáveis na substituição:
perl -i~ -pe 'BEGIN { $replace = shift }
s/_PLACEHOLDER_/$replace/g
' "$text" "$file"
Seguindo a pergunta do link
Eu tenho a seguinte string em uma variável bash
This rule forbids throwing string literals or interpolations. While JavaScript (and CoffeeScript by extension) allow any expression to be thrown, it is best to only throw <a href=\"https://developer.mozilla.org /en/JavaScript/Reference/Global_Objects/Error\"> Error</a> objects, because they contain valuable debugging information like the stack trace. Because of JavaScript's dynamic nature, CoffeeLint cannot ensure you are always throwing instances of <tt>Error</tt>. It will only catch the simple but real case of throwing literal strings. <pre> <code># CoffeeLint will catch this: throw \"i made a boo boo\" # ... but not this: throw getSomeString() </code> </pre> This rule is enabled by default.
Gostaria de substituir um texto em um arquivo por essa variável.
Atualmente, faço isso via perl -i -pe "s/_PLACEHOLDER_/$text/g" $file
mas com a estrutura do texto com "
etc, recebo
Backslash found where operator expected at -e line 6, near "Error\"
(Might be a runaway multi-line // string starting on line 1)
syntax error at -e line 6, near "Error\"
Can't find string terminator '"' anywhere before EOF at -e line 6.
Como posso substituir o texto no arquivo?
Estou assumindo que sua variável de string é uma linha longa sem novas linhas. Eu recebo um erro sed reclamando sobre uma opção desconhecida para o comando s
. Isso porque sua string contém barras, que é o delimitador do comando s
. Usando bash substituição de parâmetro para esacape as barras na string funciona.
$ cat file
this is a _PLACEHOLDER_ here
$ string="This rule forbids throwing string literals or interpolations. While JavaScript (and CoffeeScript by extension) allow any expression to be thrown, it is best to only throw <a href=\"https://developer.mozilla.org /en/JavaScript/Reference/Global_Objects/Error\"> Error</a> objects, because they contain valuable debugging information like the stack trace. Because of JavaScript's dynamic nature, CoffeeLint cannot ensure you are always throwing instances of <tt>Error</tt>. It will only catch the simple but real case of throwing literal strings. <pre> <code># CoffeeLint will catch this: throw \"i made a boo boo\" # ... but not this: throw getSomeString() </code> </pre> This rule is enabled by default."
$ $ sed "s/_PLACEHOLDER_/$string/g" file
sed: -e expression #1, char 204: unknown option to 's'
# replace '/' with '\/' globally in the string
$ sed "s/_PLACEHOLDER_/${string//\//\\/}/g" file
this is a This rule forbids throwing string literals or interpolations. While JavaScript (and CoffeeScript by extension) allow any expression to be thrown, it is best to only throw <a href="https://developer.mozilla.org /en/JavaScript/Reference/Global_Objects/Error"> Error</a> objects, because they contain valuable debugging information like the stack trace. Because of JavaScript's dynamic nature, CoffeeLint cannot ensure you are always throwing instances of <tt>Error</tt>. It will only catch the simple but real case of throwing literal strings. <pre> <code># CoffeeLint will catch this: throw "i made a boo boo" # ... but not this: throw getSomeString() </code> </pre> This rule is enabled by default. here