Re: Perl Substitution Begining Line



campbell wrote:
Thanks for the help, I never knew about $1, $2 etc.... I also
apparently read the usage of \w correctly as I thought it was matching
the whole word.

One question for you that you might have as I dig for more information
on $1 etc..., I get the error

update: Undefined variable.

perl -pi -e "s#^(\w+)$#update table = '$1'#g" file

I have narrowed the problem down to the $, which i believe is
representing end of line, so why is it having problems?


$ does mean end of line in a regexp. However, the right-hand-side of
the s/// operator is not a regexp, it's a string. And in a
double-quotish string, $ means the start of a variable. Unfortunately,
$ also means the start of a variable to your shell. You need to tell
your shell to leave that alone and pass it directly to Perl. Two ways
to do that:
If you're on Unix, you can use single-quotes to enclose the argument to
-e instead:
perl -pi -e's/^(\w+)$/update table = \'$1\'/g' file

If you're on Windows, you will probably not be able to do that, and
will instead be forced to backslash all your $ in the string:

perl -pi -e"s/^(\w+)\$/update table = '\$1'/g" file

Note that both of those are untested, so you may have to play with each
to get the escapes and quotes just right...

Paul Lalli

.



Relevant Pages

  • Re: Filter string to remove non-utf-8 characters
    ... 'The global property tells the RegExp engine to find ALL matching ... 'Our pattern tells us what to find in the string... ... 'Use the replace function of RegExp to clean the username. ...
    (microsoft.public.scripting.vbscript)
  • Re: Filter string to remove non-utf-8 characters
    ... 'The global property tells the RegExp engine to find ALL matching ... 'Our pattern tells us what to find in the string... ... 'Use the replace function of RegExp to clean the username. ...
    (microsoft.public.scripting.vbscript)
  • Re: Possible bug with StringScanner class
    ... >>> I'm not sure if this is a bug or intentional behavior, ... >> the string for matching. ... In addition, the regexp that scan ...
    (comp.lang.ruby)
  • Re: Possible bug with StringScanner class
    ... > the string for matching. ... In addition, the regexp that scan ... > Do you Yahoo!? ...
    (comp.lang.ruby)
  • Memorial Day report
    ... the pieces I'd fused with my bare match used a double string (parallel ... My film cannister shell was a test of my first use of homemade piped ... One of the stars distinctly changed from ... I get a good fire seal and fit to the mortar without noticeably ...
    (rec.pyrotechnics)