후속 질문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
나는 귀하의 문자열 변수가 개행 문자가 없는 하나의 긴 줄이라고 가정합니다. 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