6

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?

chanwcom
  • 4,420
  • 8
  • 37
  • 49

2 Answers2

4

-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.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Though the [`fileinput`](https://docs.python.org/2/library/fileinput.html) module contains much of what you need to create a one-liner or maybe two-liner. – tripleee Jul 17 '16 at 06:59
1

Perl, although a fully fledged as programming language, was initially thought, and evolved as, a tool for text manipulation.

Python, on th other hand, has always been a general purpose programing language. It can handle text and text flexibility, with enormous flexibility, when compared with, say Java or C++, but it will do so within its general syntax, without exceptions to those rules, including special syntax for regular expressions, that in absence of other directives become a program in themselves.The same goes for opening, reading and writting files, given their names.

So, you can do that with "python -c ..." to run a Python script from the command line - but your command must be a full program - with beginning, middle and end.

So, for a simple regular expression substitution in several files passed in the stdin, you could try something along:

grep foo *txt| python3 -c "import sys, re, os; [(open(filename + 'new', 'wt').write(re.sub ('foo', 'bar', open(filename).read())), os.rename(filename + "new", filename))for filename in sys.stdin]"
jsbueno
  • 99,910
  • 10
  • 151
  • 209