bash - Perl to substitute string with a string containing spaces and double quotes -


my bash script builds string variable $arraylmes, containing string like:

var availabletags=[ "01 east bering sea", "02 gulf of alaska"]; 

i need put string in javascript code, replace placeholder. thinking use like:

perl -i -pe 's/placeholder/'"${arraylmes}"'/' filename 

but command complains, because of double quotes found in string, messing bash command , in consequence perl command.

how can fix command keep spaces , double quotes?

use -s switch pass variable perl:

perl -spe 's/placeholder/$var/' -- -var="$arraylmes" filename 

the -- signifies end of command line arguments. -var="$arraylmes" sets perl variable value of shell variable $arraylmes.

alternatively, use awk:

awk -v var="$arraylmes" '{sub(/placeholder/, var)}1' filename 

a nice side-effect of using awk replacement simple string, metacharacters in original string won't interpreted.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -