フォローアップの質問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
私はあなたの文字列変数が改行文字を持たない1つの長い行であると仮定します。s
コマンドオプションが不明であるというsedエラーが発生します。これは、文字列にコマンドの区切り文字であるスラッシュが含まれているためです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