0

Following up question from https://unix.stackexchange.com/a/254637/18887

I have the following string in a bash variable

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.

I'd like to replace a text in a file with this variable.

Currently I do this via perl -i -pe "s/_PLACEHOLDER_/$text/g" $file

but with the structure of the text with " etc, I get

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.

How can I replace the text in the file?

1
  • Does your variable contain newlines? Jan 11, 2016 at 22:04

2 Answers 2

1

Pass the variable doublequoted as an argument to Perl, it can handle special characters in variables in the replacement:

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

I'm assuming your string variable is one long line without newlines. I get a sed error complaining about an unknown option to the s command. That's because your string contains slashes, which is the delimiter for the s command. Using bash parameter substitution to esacape the slashes in the string works.

$ 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
1
  • I also assume no newlines, at least it does not print any. When I use your first command I do get sed: 1: "s/_PLACEHOLDER__/This ...": bad flag in substitute command: '/'. When I use the latter one I get sed: 1: "s/_PLACEHOLDER_/This ...": bad flag in substitute command: '\' Jan 12, 2016 at 8:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .