473,729 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 2241
On Wed, 16 Feb 2005 15:18:57 +0900, Wonjae Lee <sh********@hot mail.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********@hot mail.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**@performan cedrivers.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*********@zo ran.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**@performan cedrivers.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
3758
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
1296
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 date format problem which PHP is having but I need more examples. Thank you for any help. Shufen
6
2367
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 it - following the instructiosn from http://www.vrplumber.com/programming/mstoolkit/ . It works great and I've built a windows installer for PyCrypto 2.0. I'm attempting to compile PIL for python 2.4. The build instructions for windows say :
68
5874
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
134
6098
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 different from C. I'm curious -- what is everyone's favorite trick from a non-python language? And -- why isn't it in Python? Here's my current candidate: So the other day I was looking at the language Lua. In Lua, you make a
267
10777
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 http://www.artima.com/weblogs/viewpost.jsp?thread=147358
14
5522
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
2685
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
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.