perl one liner

Discussion in 'Perl Misc' started by tweetiebirds@gmail.com, May 11, 2005.

  1. Guest

    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
     
    , May 11, 2005
    #1
    1. Advertising

  2. Joe Smith Guest

    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
     
    Joe Smith, May 11, 2005
    #2
    1. Advertising

  3. Guest

    Joe Smith <> wrote:
    > 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


    How does this change the semantics of the original?

    Hint: It does not.

    Axel
     
    , May 11, 2005
    #3
  4. On 2005-05-11, <> wrote:
    > Joe Smith <> wrote:
    >> 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

    >
    > 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

    --
    David H. Adler - <> - http://www.panix.com/~dha/
    A London.pm thread topic is like a non-alchoholic ingredient in a
    cocktail, its only there so you can pretend not to be an inebriated
    addict. - Greg McCarroll
     
    David H. Adler, May 11, 2005
    #4
  5. John Bokma Guest

    wrote:

    > Joe Smith <> wrote:
    >> 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

    >
    > How does this change the semantics of the original?
    >
    > Hint: It does not.


    Yup, now imagine:

    s/http:\/\/www.example.com\/foo\/bar\/(\w+)/http:\/\/example.com\/$1/;

    (I hope I did it right :) )

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, May 11, 2005
    #5
  6. Guest

    John Bokma <> wrote:
    >
    >> Joe Smith <> wrote:
    >>> 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

    >>
    >> How does this change the semantics of the original?
    >>
    >> Hint: It does not.


    > Yup, now imagine:


    > s/http:\/\/www.example.com\/foo\/bar\/(\w+)/http:\/\/example.com\/$1/;


    > (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
     
    , May 12, 2005
    #6
  7. Guest

    wrote:
    > 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
     
    , May 12, 2005
    #7
  8. <> wrote:
    > John Bokma <> wrote:
    >>
    >>> Joe Smith <> wrote:
    >>>> 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
    >>>
    >>> How does this change the semantics of the original?
    >>>
    >>> Hint: It does not.

    >
    >> Yup, now imagine:

    >
    >> s/http:\/\/www.example.com\/foo\/bar\/(\w+)/http:\/\/example.com\/$1/;

    >
    >> (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.



    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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 12, 2005
    #8
  9. Guest

    Tad McClellan <> wrote:
    > 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
     
    , May 12, 2005
    #9
  10. writes:
    > Tad McClellan <> wrote:
    > > 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.


    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.
     
    Arndt Jonasson, May 12, 2005
    #10
  11. John Bokma Guest

    Tad McClellan wrote:

    > 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.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, May 12, 2005
    #11
  12. John Bokma Guest

    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...

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, May 12, 2005
    #12
  13. Guest

    John Bokma <> wrote:
    > Tad McClellan wrote:
    >
    >> 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.


    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
     
    , May 13, 2005
    #13
  14. John Bokma Guest

    wrote:

    > John Bokma <> wrote:


    [ .. ]

    >> 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.


    I just reloaded the thread :-D. Yup, you're right.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, May 13, 2005
    #14
  15. Guest

    Excellent!
    Gotta love this language. thanks
     
    , May 13, 2005
    #15
  16. Guest

    thanks guys
     
    , May 13, 2005
    #16
  17. <> wrote:

    > Excellent!



    What is?


    > Gotta love this language. thanks



    Gotta hate followups that don't quote any context.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 13, 2005
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    0
    Views:
    358
    Xah Lee
    Oct 20, 2005
  2. Mark
    Replies:
    8
    Views:
    107
    Tad McClellan
    Apr 29, 2004
  3. Robert Miller

    Deciphering a very cryptic perl "one-liner"

    Robert Miller, Jun 4, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    365
    Ben Morrow
    Jun 4, 2004
  4. Larry
    Replies:
    1
    Views:
    114
    Martien Verbruggen
    Feb 3, 2005
  5. perl one liner

    , May 11, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    87
Loading...

Share This Page