473,382 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

"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
6 2213
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Olivier Scalbert | last post by:
Hello , What is the python way of doing this : perl -pi -e 's/string1/string2/' file ? Thanks Olivier
40
by: Shufen | last post by:
Hi all, Can someone who has use PHP before and know quite well about the language, tell me what are the stuffs that Python offers and PHP doesn't. A few examples will be nice. I know about the...
6
by: Fuzzyman | last post by:
I'll post this to the image-sig as well, but the audience is a bit wider here. I've just upgraded to Python 2.4. I've installed the free microsoft optimising compiler and hacked distutils to use...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
134
by: Joseph Garvin | last post by:
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
14
by: mistral | last post by:
Need compile python code, source is in html and starts with parameters: #!/bin/sh - "exec" "python" "-O" "$0" "$@" I have installed ActivePython for windows.
3
by: James Mills | last post by:
On Thu, Oct 9, 2008 at 2:26 PM, Warren DeLano <warren@delsci.comwrote: Yes it does :) I second this. It's far better to use Data Structures rather than Programming Constructs
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.