perl one liner

T

tweetiebirds

I am trying to rpelace a string in a file using a perl one liner"

I am replacing dog with cat/litter

I have tried this:

perl -pi -e 's/dog/cat\/litter/g' file


Doesnt work..

Input?

T
 
J

Joe Smith

(e-mail address removed) wrote (three times):

No need to post the same question three times. Once is sufficient.
perl -pi -e 's/dog/cat\/litter/g' file

Go and re-read the documentation on the s/// operator.
It does *not* have to use slashes!

perl -pi -e 's{dog}{cat/litter}g' file

-Joe
 
A

axel

Joe Smith said:
(e-mail address removed) wrote (three times):

No need to post the same question three times. Once is sufficient.


Go and re-read the documentation on the s/// operator.
It does *not* have to use slashes!

perl -pi -e 's{dog}{cat/litter}g' file

How does this change the semantics of the original?

Hint: It does not.

Axel
 
D

David H. Adler

How does this change the semantics of the original?

Hint: It does not.

Semantics aren't everything. Some people like their code to be more
legible/less hideous. YMMV.

dha
 
A

axel

Yup, now imagine:

(I hope I did it right :) )

I am not suggesting that / should always be used as a delimiter (as you
quite rightly show), but only that Joe Smith's 'solution' to the OPs
problem was not a maningful one.

Axel
 
A

axel

I am not suggesting that / should always be used as a delimiter (as you
quite rightly show), but only that Joe Smith's 'solution' to the OPs
problem was not a maningful one.

Or even a meaningful one...

Hint to myself: I must not read Usenet before coffee

Axel
 
T

Tad McClellan

I am not suggesting that / should always be used as a delimiter (as you
quite rightly show), but only that Joe Smith's 'solution' to the OPs
problem was not a maningful one.


There is no requirement that a followup answer, or even address,
a question that was asked.

Folks post stuff, and other folks make comments about the stuff.

Sometimes the comments answer a question, and sometimes they
are a discussion of Perl that does not answer a question.
 
A

axel

Tad McClellan said:
There is no requirement that a followup answer, or even address,
a question that was asked.

Folks post stuff, and other folks make comments about the stuff.

Sometimes the comments answer a question, and sometimes they
are a discussion of Perl that does not answer a question.

Agreed. We all do it.

But when someone says 'Go and re-read the documentation' when it
obviously not helpful, I think it is reasonable to take exception.

Axel
 
A

Arndt Jonasson

Agreed. We all do it.

But when someone says 'Go and re-read the documentation' when it
obviously not helpful, I think it is reasonable to take exception.

The person who said "Go and re-read the documentation" also suggested
a substitute for what the OP was trying. If that solved the problem,
which it very well might, the post was definitely helpful, and there is
no need to take exception to it.
 
J

John Bokma

Tad said:
Sometimes the comments answer a question, and sometimes they
are a discussion of Perl that does not answer a question.

And often it wanders off topic :-D.

To Axel, I think the reply was a good one, since it explained that you can
use other delimiters than /, and also that the unescaped / was the problem
(maybe between the lines, I am to lazy to reload the thread).

Just showing how to escape the / misses an important point: that you don't
need to use / to delimit.

Quite some beginners don't know that:

/\/\/\// can be written as, for example m{///}
"\"\"\"" can be written as, for example qq(""")

etc.

So they produce now and then hard to read code, and moreover, can't read
some parts of code written by people who do know.
 
J

John Bokma

wrote:
But when someone says 'Go and re-read the documentation' when it
obviously not helpful, I think it is reasonable to take exception.

RTFM is always a very sound advice *if* a hint of what (part of the)
documentation should be read.

Quite a lot of bad programmers are created every day because they *think*
they already know a programming language, and that a new one can be learned
by just reading the syntax a bit, especially reading it in downloaded
examples found somewhere on the net. If they know a "similar" language,
they probably even don't bother...
 
A

axel

And often it wanders off topic :-D.

Would life not be fun if we did wander off topic now and again?
To Axel, I think the reply was a good one, since it explained that you can
use other delimiters than /, and also that the unescaped / was the problem
(maybe between the lines, I am to lazy to reload the thread).

But the / was escaped in the OP.

So the problem lay elsewhere.

I know my cat would not be happy being associated with dogs :) And he is
a very bright cat who loves to sleep on top of "Learing Perl" in order
to absorb the knowledge within.
Just showing how to escape the / misses an important point: that you don't
need to use / to delimit.

Quite some beginners don't know that:

/\/\/\// can be written as, for example m{///}
"\"\"\"" can be written as, for example qq(""")
So they produce now and then hard to read code, and moreover, can't read
some parts of code written by people who do know.

Very true.

Axel
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top