Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there anything similar to "perl -pe" option in python?

Tags:

python

shell

TL;DR I want to know what is an option in Python, which might roughly correspond to the "-pe" option in Perl.

I used to use PERL for some time, but these days, I have been switching to PYTHON for its easiness and simplicity. When I needed to do simple text processing on the shell prompt, I had used perl -pe option. The following is an example.

grep foo *.txt | perl -pe 's/foo/bar/g'

To do a similar thing in Python, could someone explain to me how I can do this?

like image 546
chanwcom Avatar asked Jul 17 '16 03:07

chanwcom


People also ask

How do I run a Perl command in Python?

Your answer Open your Python code in your Python editor of choice. Go to the line in the code where you want to run your Perl script. Type "pyth. RunPerl.

What is Perl PE?

The perl -pe command. 1. Unix command to replace old date with current date dynamically.


1 Answers

-pe is a combination of two arguments, -p and -e. The -e option is roughly equivalent to Python's -c option, which lets you specify code to run on the command line. There is no Python equivalent of -p, which effectively adds code to run your passed code in a loop that reads from standard input. To get that, you'll actually have to write the corresponding loop in your code, along with the code that reads from standard input.

like image 131
BrenBarn Avatar answered Sep 28 '22 00:09

BrenBarn