Replacing text - problem with $1

  • Thread starter Martin Kristensen
  • Start date
M

Martin Kristensen

I'm trying to replace some text in some txt-files with a perl call
like this...

perl -pi -e "s/brad(.+?)bill/brian$1bob/gm;" *.txt

This should replace "brad123bill" by "brian123bob" - but instead I
just get "brianbob". What's wrong? Probably something with the $1
variable, but what?

Regards
Martin
 
A

Anno Siegel

Martin Kristensen said:
I'm trying to replace some text in some txt-files with a perl call
like this...

perl -pi -e "s/brad(.+?)bill/brian$1bob/gm;" *.txt

This should replace "brad123bill" by "brian123bob" - but instead I
just get "brianbob". What's wrong? Probably something with the $1
variable, but what?

Nothing is wrong with the regex, except for the /m modifier, which does
nothing.

Anno
 
M

Martin Kristensen

Nothing is wrong with the regex, except for the /m modifier, which does
nothing.

I solved the problem by replacing " by ' so now it works perfectly for
some reason. :)

Martin
 
H

Hunter Johnson

I'm trying to replace some text in some txt-files with a perl call
like this...

perl -pi -e "s/brad(.+?)bill/brian$1bob/gm;" *.txt

This should replace "brad123bill" by "brian123bob" - but instead I
just get "brianbob". What's wrong? Probably something with the $1
variable, but what?

Regexp is fine. By the time it gets to perl, though, the s command
looks like this:

s/brad(.+?)bill/brianbob/gm

Your shell has replaced $1 with its value (nothing) before passing it
to perl. Replacing the double quotes with single quotes should work.
If not, time to dig out the shell's documentation.

Hunter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top