regex - Why do I get "-bash: syntax error near unexpected token `('" when I run my Perl one-liner? -
this driving me insane. here's dilemma, have file in need make match. use perl , works charm in case writing shell script , reason throwing errors.
here trying match:
loop_loopstorage_rev='latest.integration' i need match loop , latest.integration.
this regex:
^(?!\#)(loop_.+rev).*[\'|\"](.*)[\'|\"]$ when use in perl script, $1 , $2 give me appropriate output. if this:
perl -nle "print qq{$1 => $2} while /^(?!#)(loop_.+rev).+?[\'|\"](.+?)[\'|\"]$/g" non-hadoop.env i error:
-bash: syntax error near unexpected token `(' i believe has beginning part of regex. real question there easier solution using sed, egrep or awk? if so, 1 know begin?
double quotes cause bash replace variable references $1 , $2 values of these shell variables. use single quotes around perl script avoid (or quote every dollar sign, backtick, etc in script).
however, cannot escape single quotes inside single quotes easily; common workaround in perl strings use character code \x27 instead. if need single-quoted perl strings, use generalized single-quoting operator q{...}.
if need interpolate shell variable name, common trick use "see-saw" quoting. string 'str'"in"'g' in shell equal 'string' after quote removal; can use adjacent single-quoted , double-quoted strings build script ... although tend rather unreadable.
perl -nle 'print "instance -> $1\nrevision -> $2" while /^(?!#)('"$name"'_.+rev).+[\x27"]([^\x27"]*)[\x27"]$/g' non-hadoop.en (notice options -nle not part of script; script quoted argument -e option. in fact perl '-nle script ...' coincidentally works, decidedly unidiomatic, point of confusing.)
Comments
Post a Comment