substitution problems using perl

Steve Lane sml at zfx.com
Thu Nov 4 16:27:30 CST 1999


On the Jax.PM jacksonville-pm-list;
Steve Lane <sml at zfx.com> wrote -


Nate wrote:
> Notice how I need the two tabs at the beggining?
> 
> I tried this:
> c:\>perl -pi.bak -e 's/^\t\t9\d*$/\t\t99110420/e' COMCITY.zone
> 
> and this
> 
> c:\>perl -pi.bak -e 's/^(\t\t9\d*)$/"\t\t99110420"/e' COMCITY.zone
> 
> What's the right way? I'm sure it's easy, but I don't see the answer.

think of what the /e is doing.  it treats the right side
as a Perl expression, evaluates it, and uses the return value
as the substitution.

the simplest would be just "s/^(\t\t9\d*)$/\t\t99110420/".
notice no /e; it's not necessary.  you only need /e if you
have a substitution that can't be expressed as a double-quoted
string, and your substitution can.

> P.S. Here's an interesting one: what if I wanted to save the matched
> number, and increment it by one?
> Something like:
> c:\>perl -pi.bak -e 's/^(\t\t9\d*)$/"$1++"/e' COMCITY.zone

you'll want to get rid of the double-quotes, as you'll
get "99110420++" as a substitution with them.  without
the quotes it should work.

i haven't tested any of this... so hopefully i'm not wrong.
i'm sure someone will correct me if i am.
--
Steve Lane <sml at zfx.com>


The Jacksonville Perl Monger's Group is operated by -
Bill -Sneex- Jones ( sneex at usa.net ),
to whom send all praises, complaints, or comments...




More information about the Jacksonville-pm mailing list