Re: Perl Substitution Begining Line
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 6 Feb 2006 09:10:58 -0800
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
.
- Follow-Ups:
- Re: Perl Substitution Begining Line
- From: campbell
- Re: Perl Substitution Begining Line
- References:
- Perl Substitution Begining Line
- From: campbell
- Re: Perl Substitution Begining Line
- From: Paul Lalli
- Re: Perl Substitution Begining Line
- From: campbell
- Perl Substitution Begining Line
- Prev by Date: Re: Perl Substitution Begining Line
- Next by Date: Re: Perl Substitution Begining Line
- Previous by thread: Re: Perl Substitution Begining Line
- Next by thread: Re: Perl Substitution Begining Line
- Index(es):
Relevant Pages
|