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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wonjae Lee

    #1

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

    I read the comment of
    http://aspn.activestate.com/ASPN/Coo.../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?



  • Stephen Thorne

    #2
    Re: "perl -p -i -e" trick in Python?

    On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee <shotgunlee@hot mail.com> wrote:[color=blue]
    > I read the comment of
    > http://aspn.activestate.com/ASPN/Coo.../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?[/color]

    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

    Comment

    • Jack Diederich

      #3
      Re: &quot;perl -p -i -e&quot; trick in Python?

      On Wed, Feb 16, 2005 at 04:38:03PM +1000, Stephen Thorne wrote:[color=blue]
      > On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee <shotgunlee@hot mail.com> wrote:[color=green]
      > > I read the comment of
      > > http://aspn.activestate.com/ASPN/Coo.../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?[/color]
      >
      > sure.
      >
      > python -c 'import os; os.system("sed -i s/change this/...tothis/g")'[/color]

      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.[color=blue][color=green][color=darkred]
      >>> ^D[/color][/color][/color]
      /tmp/> sed -i 's/change this/...tothis/g'


      -Jack

      Comment

      • Jeremy Bowers

        #4
        Re: &quot;perl -p -i -e&quot; trick in Python?

        On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee wrote:
        [color=blue]
        > I read the comment of
        > http://aspn.activestate.com/ASPN/Coo.../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?[/color]

        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.

        Comment

        • Christos TZOTZIOY Georgiou

          #5
          Re: &quot;perl -p -i -e&quot; trick in Python?

          On Wed, 16 Feb 2005 01:44:40 -0500, rumours say that Jack Diederich
          <jack@performan cedrivers.com> might have written:
          [color=blue]
          >/tmp/> python
          >Python 2.3.4 (#2, Jan 5 2005, 08:24:51)
          >Type "help", "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
          >>>> ^D[/color][/color][/color]

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

          Comment

          • Miki Tebeka

            #6
            Re: &quot;perl -p -i -e&quot; trick in Python?

            Hello Wonjae,
            [color=blue]
            > I read the comment of
            > http://aspn.activestate.com/ASPN/Coo.../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?[/color]
            See the "fileinput" module.

            HTH.
            --
            ------------------------------------------------------------------------
            Miki Tebeka <miki.tebeka@zo ran.com>

            The only difference between children and adults is the price of the toys

            Comment

            • Bart van Deenen

              #7
              :-)

              Jack Diederich <jack@performan cedrivers.com> wrote:
              [color=blue]
              > /tmp/> python
              > Python 2.3.4 (#2, Jan 5 2005, 08:24:51)
              > Type "help", "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
              > >>> ^D[/color][/color][/color]


              :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-)

              Bart

              Comment

              Working...