467,161 Members | 940 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

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

Jul 18 '05 #1
  • viewed: 1960
Share:
6 Replies
On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee <sh********@hotmail.com> 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
Jul 18 '05 #2
On Wed, Feb 16, 2005 at 04:38:03PM +1000, Stephen Thorne wrote:
On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee <sh********@hotmail.com> 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
Jul 18 '05 #3
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.
Jul 18 '05 #4
On Wed, 16 Feb 2005 01:44:40 -0500, rumours say that Jack Diederich
<ja**@performancedrivers.com> 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...
Jul 18 '05 #5
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 <mi*********@zoran.com>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys
Jul 18 '05 #6
Jack Diederich <ja**@performancedrivers.com> 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
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

52 posts views Thread by Olivier Scalbert | last post: by
40 posts views Thread by Shufen | last post: by
6 posts views Thread by Fuzzyman | last post: by
68 posts views Thread by Lad | last post: by
134 posts views Thread by Joseph Garvin | last post: by
267 posts views Thread by Xah Lee | last post: by
14 posts views Thread by mistral | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.