"perl -p -i -e" trick in Python?

Discussion in 'Python' started by Jeremy Bowers, Feb 16, 2005.

  1. On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee wrote:

    > I read the comment of
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277753.
    > (Title : Find and replace string in all files in a directory)
    >
    > "perl -p -i -e 's/change this/..to this/g'" trick looks handy.
    > Does Python have a similar trick? Or, is there a shorter Python recipe for
    > the given problem?


    As a Python lover... I still tend to use "perl -pi -e", except in rare
    cases where I either can't deal with or don't want to deal with the
    necessary escaping, in which case I write a quick perl script like this
    (just did this today):

    #!/usr/bin/perl
    $source = join "", <>;
    $source =~ s/\"\"\".*?\"\"\"[ \n]*//gs;
    print $source;

    While a Python-golf contest might be able to beat that (although,
    truthfully, to match this feature for feature I'd be surprised... that <>
    is a substantial whack of code to fully emulate and I use this both as a
    pipe and by feeding it a long list of files as arguments), I still
    couldn't have written it as quickly.

    Upshot is, perl is good for something, and when I'm not doing the job I
    have working with perl, I'll still reach for perl -pi -e without shame.
    Well, actually, only with the shame that I really need to lookup the
    command to save backups and start using it. ("man perlrun"... I know where
    to find it, I just need to add it to muscle memory!) Much longer than this
    though and I drop the perl and run away, if possible.
     
    Jeremy Bowers, Feb 16, 2005
    #1
    1. Advertising

  2. Jeremy Bowers

    Wonjae Lee Guest

    Wonjae Lee, Feb 16, 2005
    #2
    1. Advertising

  3. On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee <> wrote:
    > I read the comment of
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277753.
    > (Title : Find and replace string in all files in a directory)
    >
    > "perl -p -i -e 's/change this/..to this/g'" trick looks handy.
    > Does Python have a similar trick? Or, is there a shorter Python recipe for
    > the given problem?


    sure.

    python -c 'import os; os.system("sed -i s/change this/...tothis/g")'

    Using-the-right-tool-for-the-job-ly-y'rs
    Stephen
     
    Stephen Thorne, Feb 16, 2005
    #3
  4. On Wed, Feb 16, 2005 at 04:38:03PM +1000, Stephen Thorne wrote:
    > On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee <> wrote:
    > > I read the comment of
    > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277753.
    > > (Title : Find and replace string in all files in a directory)
    > >
    > > "perl -p -i -e 's/change this/..to this/g'" trick looks handy.
    > > Does Python have a similar trick? Or, is there a shorter Python recipe for
    > > the given problem?

    >
    > sure.
    >
    > python -c 'import os; os.system("sed -i s/change this/...tothis/g")'


    You beat me to it, but you can cut a few more characters out of that.

    /tmp/> python
    Python 2.3.4 (#2, Jan 5 2005, 08:24:51)
    Type "help", "copyright", "credits" or "license" for more information.
    >>> ^D

    /tmp/> sed -i 's/change this/...tothis/g'


    -Jack
     
    Jack Diederich, Feb 16, 2005
    #4
  5. On Wed, 16 Feb 2005 01:44:40 -0500, rumours say that Jack Diederich
    <> might have written:

    >/tmp/> python
    >Python 2.3.4 (#2, Jan 5 2005, 08:24:51)
    >Type "help", "copyright", "credits" or "license" for more information.
    >>>> ^D


    "Instant porting of any program to python". Smooth.
    --
    TZOTZIOY, I speak England very best.
    "Be strict when sending and tolerant when receiving." (from RFC1958)
    I really should keep that in mind when talking with people, actually...
     
    Christos TZOTZIOY Georgiou, Feb 16, 2005
    #5
  6. Jeremy Bowers

    Miki Tebeka Guest

    Hello Wonjae,

    > I read the comment of
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277753.
    > (Title : Find and replace string in all files in a directory)
    >
    > "perl -p -i -e 's/change this/..to this/g'" trick looks handy.
    > Does Python have a similar trick? Or, is there a shorter Python recipe for
    > the given problem?

    See the "fileinput" module.

    HTH.
    --
    ------------------------------------------------------------------------
    Miki Tebeka <>
    http://tebeka.bizhat.com
    The only difference between children and adults is the price of the toys
     
    Miki Tebeka, Feb 16, 2005
    #6
  7. :)

    Jack Diederich <> wrote:

    > /tmp/> python
    > Python 2.3.4 (#2, Jan 5 2005, 08:24:51)
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> ^D



    :) :) :) :) :) :) :) :) :) :) :) :) :)

    Bart
     
    Bart van Deenen, Feb 17, 2005
    #7
    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. Robert Brewer

    derivation: sick Python trick of the week

    Robert Brewer, Feb 23, 2004, in forum: Python
    Replies:
    5
    Views:
    414
  2. Doug Holton
    Replies:
    20
    Views:
    832
    gabriele renzi
    Jun 26, 2004
  3. Dan
    Replies:
    15
    Views:
    483
    Peter Otten
    Aug 11, 2004
  4. Joseph Garvin

    Favorite non-python language trick?

    Joseph Garvin, Jun 24, 2005, in forum: Python
    Replies:
    138
    Views:
    2,068
    Robert Kern
    Jul 7, 2005
  5. Steve
    Replies:
    1
    Views:
    327
    Roy Smith
    Jun 25, 2005
Loading...

Share This Page