perl -pi.bak -e equivalent

C

Caj Zell

Hello,

I am looking a little bit at python and am curious of one thing I
often use in perl. Suppose, I want to change the string
"best_composer" to "Zappa" in a document called facts.txt, then in
perl I would do

perl -pi.bak -e "s/best_composer/Zappa/;" facts.txt

Can I do that with python from the command line so easily?

Caj Zell
 
D

Diez B. Roggisch

Caj said:
Hello,

I am looking a little bit at python and am curious of one thing I
often use in perl. Suppose, I want to change the string
"best_composer" to "Zappa" in a document called facts.txt, then in
perl I would do

perl -pi.bak -e "s/best_composer/Zappa/;" facts.txt

Can I do that with python from the command line so easily?

Short answer: No. Perl is designed for this kind of stuff. Python won't
catch up in that field, for various reasons. There have been plenty of
discussions on the subject of one-liners in python - googl.groups will show
you to them.

As always, its important to use the right tool for the job - and in this
case, I'd even go for sed :)

sed s/best_composer/Zappa/ < facts.txt > pi.bak

is even shorter...
 
G

gabriele renzi

Diez B. Roggisch ha scritto:
Caj Zell wrote:




Short answer: No. Perl is designed for this kind of stuff. Python won't
catch up in that field, for various reasons. There have been plenty of
discussions on the subject of one-liners in python - googl.groups will show
you to them.

As always, its important to use the right tool for the job - and in this
case, I'd even go for sed :)

sed s/best_composer/Zappa/ < facts.txt > pi.bak

is even shorter...

let me suggest pyone:
http://www.unixuser.org/~euske/pyone/

It is really an erethic twist of python, anyway

PS
the perl code does not do the same thing than the sed one, it creates a
backup file named facts.txt.bak and changes facts.txt
 
C

Caj Zell

Diez B. Roggisch said:
Short answer: No. Perl is designed for this kind of stuff. Python won't
catch up in that field, for various reasons. There have been plenty of
discussions on the subject of one-liners in python - googl.groups will show
you to them.


OK, I did some googling on "perl command line -e python equivalent"
and various combinations, but didn't think of the obvious "perl
one-liner python equivalent" your answer implies. With that I want to
say that I did try before posting here.

Nice to see noone challenges the musical meaning of my search and
replace example, seems like you pythoners have pretty good taste.

Caj Zell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,275
Latest member
Michaelachoda

Latest Threads

Top