Is there something similar to the "perl -pe" parameter in python?

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

I used PERL for some time, but these days I switched to PYTHON for its lightness and simplicity. When I needed to do simple text processing on the command line, I used the perl -pe option. The following is an example.

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

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

+4
source share
2 answers

-pe -p -e. -e Python -c, . Python -p, , . , , .

+3

Perl, , .

Python, , . , , Java ++, , , . , , .

, "python -c..." Python script , - , .

, , stdin, -:

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]"
+1

Source: https://habr.com/ru/post/1648123/


All Articles