perl:用双引号替换文件中的字符串

perl:用双引号替换文件中的字符串

跟进问题https://unix.stackexchange.com/a/254637/18887

我在 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.

我想用这个变量替换文件中的文本。

目前我通过这样做perl -i -pe "s/_PLACEHOLDER_/$text/g" $file

但根据文本的结构"等,我得到

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.

如何替换文件中的文本?

答案1

将带双引号的变量作为参数传递给 Perl,它可以在替换中处理变量中的特殊字符:

perl -i~ -pe 'BEGIN { $replace = shift }
              s/_PLACEHOLDER_/$replace/g
             ' "$text" "$file"

答案2

我假设您的字符串变量是一长行,没有换行符。我收到 sed 错误,抱怨s命令的选项未知。这是因为您的字符串包含斜杠,斜杠是命令的分隔符s。使用 bash 参数替换来转义字符串中的斜杠是可行的。

$ 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

相关内容