How do I put apostraphes in this expression?

L

laredotornado

Hi, I'm using Perl 5.8.9 on Mac 10.6.3. I regularly run a search and
replace expression from the terminal, but when the replacing
expression contains an apostraphe, the replace doesn't work.
Specifically, I have

perl -pi -e 's/(\d\d\d)/\'$1\'/g' /tmp/output

but this is apparently not the right way to add apostraphes. What
would be the right way? Thanks, - Dave
 
M

Marc Girod

but this is apparently not the right way to add apostraphes.  What
would be the right way?  Thanks, - Dave

Apostrophes. Or single quotes.
But this is a shell problem more than a Perl problem.

Two solutions come to my mind:

perl -pi -e 'BEGIN{$q=chr(39)}s/(\d\d\d)/$q$1$q/g' /tmp/output
perl -pi -e 's/(\d\d\d)/chr(39) . $1 . chr(39)/eg' /tmp/output

Marc
 
L

laredotornado

Apostrophes. Or single quotes.
But this is a shell problem more than a Perl problem.

Two solutions come to my mind:

perl -pi -e 'BEGIN{$q=chr(39)}s/(\d\d\d)/$q$1$q/g' /tmp/output
perl -pi -e 's/(\d\d\d)/chr(39) . $1 . chr(39)/eg' /tmp/output

Marc

Thanks. Regarding the first solution, I tried what you suggested and
got a pile of compilation errors. I'm using bash shell. Here's the
output ...

davea-mbp2:~ davea$ perl -pi -e 's/BEGIN{$q=chr(39)}s/(.*),(.*)/$q$1$q/
g' /tmp/output
Scalar found where operator expected at -e line 1, near "s/
BEGIN{$q=chr(39)}s/(.*),(.*)/$q"
Scalar found where operator expected at -e line 1, near "$q$1"
(Missing operator before $1?)
Scalar found where operator expected at -e line 1, near "$1$q"
(Missing operator before $q?)
syntax error at -e line 1, near "s/BEGIN{$q=chr(39)}s/(.*),(.*)/$q"
Execution of -e aborted due to compilation errors.

Any additional thoughts are appreciated, - Dave
 
P

Peter J. Holzer

^^
The first two characters are "BE" here.

^^
The first two characters are "s/" here.


You should copy/paste code rather than (attempt to) retype it.

That's good advice for posting code.

However, for using code which is posted into this group the most
important thing is to understand it. Retyping may actually be useful
here as it forces you to look at the code (although that clearly didn't
help here).

hp
 
R

Ralph Malph

On 12/20/2010 5:50 PM, Tad McClellan wrote:
[snip]
Have you seen the Posting Guidelines that are posted here frequently?
I am sure he has "seen" them. Of course, since what you post is
unreadable shit I am sure the contents or even the intent is a complete
mystery to most everyone that has posted here in the past 10 years.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top