Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > "perl -p -i -e" trick in Python?

Reply
Thread Tools

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

 
 
Jeremy Bowers
Guest
Posts: n/a
 
      02-16-2005
On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee wrote:

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


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.
 
Reply With Quote
 
 
 
 
Wonjae Lee
Guest
Posts: n/a
 
      02-16-2005
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?



 
Reply With Quote
 
 
 
 
Stephen Thorne
Guest
Posts: n/a
 
      02-16-2005
On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee <(E-Mail Removed)> wrote:
> 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?


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
 
Reply With Quote
 
Jack Diederich
Guest
Posts: n/a
 
      02-16-2005
On Wed, Feb 16, 2005 at 04:38:03PM +1000, Stephen Thorne wrote:
> On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee <(E-Mail Removed)> wrote:
> > 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?

>
> 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
 
Reply With Quote
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      02-16-2005
On Wed, 16 Feb 2005 01:44:40 -0500, rumours say that Jack Diederich
<(E-Mail Removed)> 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 RFC195
I really should keep that in mind when talking with people, actually...
 
Reply With Quote
 
Miki Tebeka
Guest
Posts: n/a
 
      02-16-2005
Hello Wonjae,

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

See the "fileinput" module.

HTH.
--
------------------------------------------------------------------------
Miki Tebeka <(E-Mail Removed)>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys
 
Reply With Quote
 
Bart van Deenen
Guest
Posts: n/a
 
      02-17-2005
Jack Diederich <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
profile trick, really anoying Splibbilla Firefox 2 06-29-2005 08:39 PM
so... what's the trick in getting wireless working on Server 2003?! pif Wireless Networking 1 10-13-2004 04:21 PM
A .Net trick everday! Nuri YILMAZ ASP .Net 0 08-09-2004 07:58 AM
A .Net trick everyday! Nuri Yilmaz ASP .Net 0 07-28-2004 01:46 PM
Is this trick with reset acceptable? valentin tihomirov VHDL 6 04-14-2004 05:52 PM



Advertisments